Format Strings

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi,

Anyone know why "s" equals "0.0870000000000000000000" after the following
lines execute?

Thanks,
Nate

double d = 0.087000000000000008;
string s = string.Format("{0:f22}", d);
 
"0870000000000000000000" has length of 22

0:f# -> Fixed point with # length, unspecified # is 2 i think


if you just want 0.87
then just use 0:f
 
Nathan said:
Anyone know why "s" equals "0.0870000000000000000000" after the following
lines execute?
double d = 0.087000000000000008;
string s = string.Format("{0:f22}", d);

double only has "15-16 digits" precision: the 8 in the 17th place is
lost.
 
After execution, I would expect "s" to equal:

"0.0870000000000000080000"

(note the super-important 8 in the 18th digit after the decimal)...

If you run these lines in the debugger, you will see that "d" equals
0.087000000000000008, but that is not what "string.Format(...)" spits out.
I'm calling this a bug until someone proves otherwise...

Thanks,
Nate
 
That's where I started in my thought process, too. However, in practice the
8 is NOT lost. Try running these lines in a debugger and observe...

Thanks,
Nate
 
That's where I started in my thought process, too. However, in practice the
8 is NOT lost. Try running these lines in a debugger and observe...

I believe the 8 is not lost because the first zero after the decimal is NOT
stored.

Thanks,
Nate
 
Nathan said:
That's where I started in my thought process, too. However, in practice the
8 is NOT lost. Try running these lines in a debugger and observe...

Presumably, the debugger sees the value on the FPU stack. But, also
presumably, double.ToString() uses only 64-bits, not 80.
 
Nathan,

Read the documentation on "double data type". It says "Precision: 15-16
digits".

I'd use decimal if I was you...

HTH,
Alexander
 
Even after you drop the initial zero you still have 16 digits. I think if
you look at the documentation the digits of precision is 15. So you might be
losing the last digit. As Alexander recommends, you might want to consider
using a decimal type.
Mark
 
The reason lies in how the double type used the 64 bits it has available to
store the decimal value you are dealing with.

The VS debugger incorrectly displays the last 8 even if it cannot be sure
that the last digit is an 8. It cannot be sure that the last number is an 8
because that's the point where the precision of doubles ends and why the
precision is so weirdly expressed as 16-17 digits.

Try the following code and see what the debugger displays for d:

double d = 0.087000000000000001;
string s = string.Format("{0:f22}", d);

The debugger incorrectly displays 0.087000000000000008 trying to show too
much precision which double does not have. The String.Format correctly stops
displaying the last digit where the double's precision ends.
 
Back
Top