C # decimal separator?

I have a method that returns numbers like this:

public decimal GetNumber() { return 250.00m; } 

Now, when this value is printed on the console, for example, it has a comma (250.00) instead of a period (250.00). I always want here, what am I doing wrong?

+12
decimal c # separator
Oct 06 2018-10-10T00:
source share
4 answers

decimal itself does not have formatting - it has neither a comma nor a period.

This is when you convert it to the string you get. You can make sure you get the point by specifying an invariant culture:

 using System; using System.Globalization; using System.Threading; class Test { static void Main() { Thread.CurrentThread.CurrentCulture = new CultureInfo("fr-FR"); decimal d = 5.50m; string withComma = d.ToString(); string withDot = d.ToString(CultureInfo.InvariantCulture); Console.WriteLine(withComma); Console.WriteLine(withDot); } } 
+48
Oct 06 2018-10-10T00:
source share

As John Skeet explained, you must specify the culture used to format the string :

 var str = GetNumber().ToString(System.Globalization.CultureInfo.InvariantCulture); 

It’s good practice to always use the ToString overload in which you specify the culture. Otherwise, .NET use the current Culture stream, which will write different strings for output according to the PC locale ...

+6
Oct 06 '10 at 7:13
source share

Locale specific formatting?

http://en.wikipedia.org/wiki/File:DecimalSeparator.svg (green means a comma, so if you call ToString() in your decimal value using the culture information of any of these locations, you will see a comma).

+1
Oct 06 2018-10-10T00:
source share

I checked it with visual studio 2008 (console application) and did not show "," instead of "."., Please provide more details. I think this is a matter of culture-information. provide additional information about codes

 class Program { static void Main(string[] args) { Console.Write(GetNumber()); } public static decimal GetNumber() { return 250.00m; } } 
0
Oct 06 '10 at 7:15
source share



All Articles