91

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

Community
  • 1
  • 1
Captain Comic
  • 15,744
  • 43
  • 110
  • 148

5 Answers5

172

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"
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Guffa
  • 687,336
  • 108
  • 737
  • 1,005
  • What about thousand separator? What controls it? – Captain Comic Nov 02 '10 at 10:37
  • Please tell me how "0.00" is parsed by .NET – Captain Comic Nov 02 '10 at 10:38
  • 2
    @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. – Guffa Nov 02 '10 at 10:42
  • Guffa, so "0.00" assumes no thousand separator? I guess I need to do a good RTFM on format specifiers. – Captain Comic Nov 02 '10 at 10:46
  • 1
    Here you can find a good documentation about it. https://learn.microsoft.com/en-us/dotnet/standard/base-types/standard-numeric-format-strings – Jonatan Machado Jul 15 '22 at 23:02
59

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"

horgh
  • 17,918
  • 22
  • 68
  • 123
Pedro Santos
  • 591
  • 4
  • 2
11

You can use

value.ToString(CultureInfo.InvariantCulture)

to get exact double value without putting precision.

Mahi
  • 1,019
  • 9
  • 19
  • 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. – Gonzo Gonzales Aug 31 '20 at 06:18
3

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

Makah
  • 4,435
  • 3
  • 47
  • 68
  • 2
    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. – Carl Oct 01 '14 at 16:02
  • OP wanted "NO thousands separator". – Thomas Weller Jun 23 '22 at 07:58
-6

You can simply use decimal.ToString()

For two decimals

myDecimal.ToString("0.00");

For four decimals

myDecimal.ToString("0.0000");

This gives dot as decimal separator, and no thousand separator regardless of culture.

Øyvind Bråthen
  • 59,338
  • 27
  • 124
  • 151
  • 13
    No, it gives the decimal separator specified by the culture, not always period. – Guffa Nov 02 '10 at 10:34
  • 4
    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. – LukeH Nov 02 '10 at 10:36
  • 1
    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? – Øyvind Bråthen Nov 02 '10 at 10:41
  • 3
    @Ø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. – Guffa Nov 02 '10 at 10:48
  • 2
    @Guffa: This I need to research further so I know what's going on. Thanks for your pointers here. – Øyvind Bråthen Nov 02 '10 at 11:00