C# decimal separator?

只谈情不闲聊 提交于 2019-11-28 09:50:39

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

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...

Locale-specific formatting?

http://en.wikipedia.org/wiki/File:DecimalSeparator.svg (Green equals a comma, so if you are calling ToString() on your decimal using the culture info of any of these locations, you will see a comma).

I have checked it with visual studio 2008 (console application) and its not showing "," instead of "." , please provide more details. I think its issue of culture-info. please provide some more code details

class Program
{
    static void Main(string[] args)
    {
        Console.Write(GetNumber());
    }
    public static  decimal GetNumber()
    {
        return 250.00m;
    }

}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!