C# decimal separator?
decimal
itself doesn't have formatting - it has neither a comma nor a dot.
It's when you convert it to a string that you'll get that. You can make sure you get a dot by specifying the 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);
}
}
As explained by Jon Skeet, you should specify the culture used to format the string
:
var str = GetNumber().ToString(System.Globalization.CultureInfo.InvariantCulture);
It's a good practice to always use the ToString
overload in which you specify the culture. Otherwise, .NET
use the current thread Culture
, which would write different strings
to the output according to the locale of the PC
...