Converting double to string with N decimals, dot as decimal separator, and no thousand separator

149,921

Solution 1

For a decimal, use the ToString method, and specify the Invariant culture to get a period as decimal separator:

value.ToString("0.00", System.Globalization.CultureInfo.InvariantCulture)

The long type is an integer, so there is no fraction part. You can just format it into a string and add some zeros afterwards:

value.ToString() + ".00"

Solution 2

It's really easy to specify your own decimal separator. Just took me about 2 hours to figure it out :D. You see that you were using the current ou other culture that you specify right? Well, the only thing the parser needs is an IFormatProvider. If you give it the CultureInfo.CurrentCulture.NumberFormat as a formatter, it will format the double according to your current culture's NumberDecimalSeparator. What I did was just to create a new instance of the NumberFormatInfo class and set it's NumberDecimalSeparator property to whichever separator string I wanted. Complete code below:

double value = 2.3d;
NumberFormatInfo nfi = new NumberFormatInfo();
nfi.NumberDecimalSeparator = "-";
string x = value.ToString(nfi);

The result? "2-3"

Solution 3

You can use

value.ToString(CultureInfo.InvariantCulture)

to get exact double value without putting precision.

Solution 4

I prefer to use ToString() and IFormatProvider.

double value = 100000.3
Console.WriteLine(value.ToString("0,0.00", new CultureInfo("en-US", false)));

Output: 10,000.30

Share:
149,921
Captain Comic
Author by

Captain Comic

I am interested in C#, .NET, algorithms, and finance.

Updated on July 05, 2022

Comments

  • Captain Comic
    Captain Comic almost 2 years

    I need to convert a decimal to a string with N decimals (two or four) and NO thousand separator:

    'XXXXXXX (dot) DDDDD'

    The problem with CultureInfo.InvariantCulture is that is places ',' to separate thousands.

    UPDATE

    This should work for decimal and double types.


    My previous question: Need to convert double or decimal to string

  • Guffa
    Guffa over 13 years
    No, it gives the decimal separator specified by the culture, not always period.
  • LukeH
    LukeH over 13 years
    The . in your format string is a placeholder for the decimal separator and not a literal .. So if, for example, your culture is fr-FR then the number will be displayed as 1,23 or 1,2345 etc.
  • Captain Comic
    Captain Comic over 13 years
    What about thousand separator? What controls it?
  • Captain Comic
    Captain Comic over 13 years
    Please tell me how "0.00" is parsed by .NET
  • Øyvind Bråthen
    Øyvind Bråthen over 13 years
    I tried changing the decimal char under Regional settings in Windows to comma, and it still print dot and not comma. Any reason why it don't change then, if it's just a placeholder?
  • Guffa
    Guffa over 13 years
    @Captain Comic: You get thousands separator when you for example use the format "N2", but not with the custom format "0.00". The custom format "0.00" means "one or more digits before the decimal separator, the culture dependant decimal separator, and exactly two digits after the decimal separator". By specifying the invariant culture it's decimal separator is used, which is a period.
  • Captain Comic
    Captain Comic over 13 years
    Guffa, so "0.00" assumes no thousand separator? I guess I need to do a good RTFM on format specifiers.
  • Guffa
    Guffa over 13 years
    @Øyvind Bråthen: Your personal culture setting doesn't affect the application because it gets the setting from somewhere else, which depends on what type of application it is.
  • Øyvind Bråthen
    Øyvind Bråthen over 13 years
    @Guffa: This I need to research further so I know what's going on. Thanks for your pointers here.
  • Carl
    Carl over 9 years
    This will cause problems. If you override your decimal symbol to a comma, this will output 100,000,30. This is because the constructor for CultureInfo you are using will respect user settings such as decimal symbol. A safer way is to either use CultureInfo.Invariant, or you can construct it like this: new CultureInfo("en-US", false) Passing false to the constructor tells it not to respect the user customizations.
  • Gonzo Gonzales
    Gonzo Gonzales over 3 years
    You need to be carefull with that because if the programm runs on a programm with a diffrent Culture it can lead to Problems if you did not set the Zone.
  • Thomas Weller
    Thomas Weller almost 2 years
    OP wanted "NO thousands separator".