Double.ToString returns blank for 0
From "#"
Custom specifier
Note that this specifier never displays zero that is not a significant digit , even if zero is the only digit in the string . This will show zero only if it is a significant digit in the number displayed.
If you want to render it as 00,000,000,000
, you can use "0"
Custom Specifier instead . But remember that this specifier puts zero if it doesn't match any numeric value in your double.
This means it (15).ToString("00,000,000,000")
will generate 00,000,000,015
.
If you want to display 0
, and not your other values, just change the format of the last digit from #
to 0
, like ##,###,###,##0
.
But the best way might be to use a Numeric format specifier"N"
in my opinion. These specifiers generate thousands of group separators and size, decimal separator and its size (you can omit this part with N0
) possible negative sign and its pattern.
double d = 0;
var text = d.ToString("N0");
Also I want to mention about ,
in your string format. You want to group as a thousand separator (and since you are not using IFormatProvider
in your code), if yours is different from a character , that character will be, not .CurrentCulture
NumberGroupSeparator
,
,
For example; I am in Turkey, and my current culture tr-TR
, and this culture has .
both NumberGroupSeparator
. Therefore your code will generate the result on my machine in ##.###.###.###
not format ##,###,###,###
.
source to share