Formatting Numbers as Strings with Commas in place of Decimals

后端 未结 9 584
离开以前
离开以前 2020-12-03 07:46

I have the following number: 4.3

I\'d like to display this number as 4,3 for some of our European friends.

I was under the impressi

9条回答
  •  失恋的感觉
    2020-12-03 08:37

    Yes, you are using the wrong string, and also the problem can't be solved by only providing a formatting string.

    What your formatting string does is to format the number using the pattern "0", then aligned to the length 0.

    When you specify the decimal separator in a formatting string it's always a period regardless of the current culture. The decimal separator in the result of the formatting on the other hand is always the one used by the current culture. So, to get a comma as decimal separator in the result you have to make sure that the culture used for the formatting is one that uses comma as decimal separator.

    You can either set the current culture for the thread so that it's used by default by the formatting, or specify a culture in the call:

    string ret = String.Format(CultureInfo.GetCultureInfo(1053), "{0:0.0}", 4.3);
    

提交回复
热议问题