Reputation: 25
Why does the following program not output the negative sign for the second line?
var smallpos = 3.65433E-005;
var smallneg = -3.65433E-005;
Console.WriteLine("{0} in F4 format with a width of 8 characters {1}",
smallpos,
smallpos.ToString("F4").PadLeft(8).Substring(0, 8));
Console.WriteLine("{0} in F4 format with a width of 8 characters {1}",
smallneg,
smallneg.ToString("F4").PadLeft(8).Substring(0, 8));
Using VS 2017 Professional 15.8.2 C# 7.2
Upvotes: 0
Views: 861
Reputation: 19101
Once you've eliminated enough digits from the back, the remaining number will have a value of zero, at which point the existence of a "-
" has no real meaning.
This can be understood quite intuitively by running the snippet below, which has a decreasing number provided for the fixed-point specifier.
In the last line, that specifier is omitted, at that point NumberFormatInfo.NumberDecimal decides the number of decimal places used (depending on the culture used):
var smallneg = -3.65433E-005;
Console.WriteLine(smallneg.ToString("F10")); // -0,0000365433
Console.WriteLine(smallneg.ToString("F9")); // -0,000036543
Console.WriteLine(smallneg.ToString("F8")); // -0,00003654
Console.WriteLine(smallneg.ToString("F7")); // -0,0000365
Console.WriteLine(smallneg.ToString("F6")); // -0,000037
Console.WriteLine(smallneg.ToString("F5")); // -0,00004
Console.WriteLine(smallneg.ToString("F4")); // 0,0000 <-- Zero --> (-0) == 0
Console.WriteLine(smallneg.ToString("F")); // 0,00
Upvotes: 1
Reputation: 14034
-3.65433E-005
represents -0.0000365433
.
The issue here is with smallneg.ToString("F4")
. It only considers the first 4 places after the decimal point; since they're all 0 the negative sign is left out as -0 wouldn't make much sense.
Upvotes: 3