C# - Force String.Format to use decimals and NEVER commas
Asked Answered
J

2

6

Basically I'm realizing that my application is using commas instead of decimals, and i NEVER want to allow this. Anyone know how I can correct? I can't find one thing via google that is to force decimals, it's all about forcing commas.

         return String.Format("{0}f, {1}f, {2}f, {3}f, {4}f, {5}f, {6}f, {7}f, {8}f, {9}f, {10}f, {11}f, {12}f, {13}f, {14}f, {15}f", M.M11, M.M12, M.M13, M.M14, M.M21, M.M22, M.M23, M.M24, M.M31, M.M32, M.M33, M.M34, M.OffsetX, M.OffsetY, M.OffsetZ, M.M44);
Jerriejerrilee answered 19/2, 2013 at 0:18 Comment(3)
Can you give us an example of when it's using commas and it should be using decimals?Tristis
Like if someone in the UK starts my application, 1.5654654 will be 1,5654654Jerriejerrilee
If you don't allow that, it will confuse the hell out of your UK users. Are you sure that is going to be ok?Tristis
H
9

You can use the other overload:

return String.Format(
    CultureInfo.InvariantCulture // <<== That's the magic
,   "{0}f, {1}f, {2}f, {3}f, {4}f, {5}f, {6}f, {7}f, {8}f, {9}f, {10}f, {11}f, {12}f, {13}f, {14}f, {15}f"
,   M.M11, M.M12, M.M13, M.M14, M.M21, M.M22, M.M23, M.M24, M.M31, M.M32, M.M33, M.M34, M.OffsetX, M.OffsetY, M.OffsetZ, M.M44
);

This way of calling ensures that the invariant culture is being passed as the format provider to the String.Format, ensuring that you get dots for numbers, dollars for currency symbols, English for names of the months and days, and so on.

Hayley answered 19/2, 2013 at 0:24 Comment(0)
I
1

Try setting the culture to US English for the String.Format function:

String.Format(new CultureInfo("en-US"), "{0}f, {1}f, {2}f", etc)
Indication answered 19/2, 2013 at 0:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.