0

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

1
  • 1
    What is the output you are getting and what you were expected? Commented Aug 31, 2018 at 4:43

2 Answers 2

3

-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.

Sign up to request clarification or add additional context in comments.

1 Comment

You could enforce it with a custom format, if I remember correctly, though.
1

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

3 Comments

"at which point the existence of a "-" has no real meaning." I disagree with that. If you are aware that the displayed number has been rounded, then it would indicate the original (exact) number was < 0 ... - I don't know if there are in fact real life applications, where this would be interesting or important, but there is always a first time for everything.
@Fildor I see your point, but note that the resulting output of smallneg.Tostring() is not a number, but a string, and in the context of that string, any information about eliminated digits has already been lost.
Of course. Just saying it could have a meaning if it was possible to have it. Though I admit in those cases you'd probably rather have the exact value than just a "hint" that the exact value was a tiny bit smaller than 0 ...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.