Formatting Numbers as Strings with Commas in place of Decimals

后端 未结 9 581
离开以前
离开以前 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

    This depends on what you want to do. If you want to make your entire application ready for some other language output just use

    System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo ("de-DE");
    

    If you want to have a mixed language application (that means some of the outputs are formatted in one language some in another) you have to use the overloads of the specific String functions like:

    var culture =  System.Globalization.CultureInfo.GetCultureInfo("de-DE");   
    String.Format (culture, "{0:0.0}", 4.3);
    

    The first method is preferred if it is possible because you only need one single line in your application and everything will be as expected. In the second you have to add the culture parameter to every String ouput method in your entire application.

提交回复
热议问题