C# decimal separator?

grady picture grady · Oct 6, 2010 · Viewed 46.2k times · Source

I have a method which returns numbers like this:

public decimal GetNumber()
{
    return 250.00m;
}

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

Answer

Jon Skeet picture Jon Skeet · Oct 6, 2010

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);
    }
}