2

Why does this simple Console App Output 100000000000000 instead of 99999999999999.99 or atleast 99999999999999.984 as the debugger is showing?

static void Main(string[] args)
{
    double bigNum = 99999999999999.99;
    Console.WriteLine(bigNum); //Console.WriteLine will internally ToString, but you can add what ever ToString() you want here the result will always be undesired
}

Console Output:

Nope

Debug Output:

For clarification:

Yes, I know of the value type decimal and its superiour precision over double, but I never heard of that ToString() of double could give wrong results.

Rand Random
  • 7,300
  • 10
  • 40
  • 88
  • 7
    define "wrong", it has an accuracy of 0.00000000000001% ;-) – Stefan Apr 26 '18 at 14:49
  • Possible duplicate of [Is floating point math broken?](https://stackoverflow.com/questions/588004/is-floating-point-math-broken) – Robert Columbia Apr 26 '18 at 14:49
  • `double` has limited precision, so it will only store the first 15-16 digits of your number. For more precision, use `System.Numerics.BigInteger` - it doesn't support non-integers though, so you must add the decimal points yourself (if you have a fixed number of decimals). – Didix Apr 26 '18 at 14:51
  • 3
    Try using `bigNum.ToString("R")` or `Console.WriteLine("{0:R}", bigNum);` – Matthew Watson Apr 26 '18 at 14:51
  • 1
    @RobertColumbia I think the question is different - "why debugger does not match `double.ToString("G")`" rather than generic explanation of double's behavior. (Note that https://learn.microsoft.com/en-us/dotnet/standard/base-types/standard-numeric-format-strings#GFormatString tells you to use `"G17"` if you want to keep all digits, by default `"G"` is `"G15"`) – Alexei Levenkov Apr 26 '18 at 14:52

1 Answers1

2

See: MSDN for the standard ToString conversion:

If format is null or an empty string, the return value is formatted with the general numeric format specifier ("G").

This will show you, that the defaulted number format specifier "G", will default to max 15 digits if possible: G-format specifier

As requested:

double bigNum = 99999999999999.99;

Console.WriteLine(bigNum);                 //default
//100000000000000
Console.WriteLine(bigNum.ToString("G"));   //explicit default
//100000000000000
Console.WriteLine(bigNum.ToString("G17")); //G17
//99999999999999.984
Stefan
  • 17,448
  • 11
  • 60
  • 79