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

Captain Comic picture Captain Comic · Nov 2, 2010 · Viewed 128.4k times · Source

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

Answer

Guffa picture Guffa · Nov 2, 2010

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"