467,877 Members | 1,130 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 467,877 developers. It's quick & easy.

Problem using string.Format to show a double

Boz
Hi,

I am trying to use string.Format() to output the value of a double.

double da = 100000000000.99994;
double db = 100000000000.9994;
double dc = 100000000000.994;
double dd = 1000000000.99994;

Debug.WriteLine(string.Format("a={0:F10}", da));
Debug.WriteLine(string.Format("b={0:F10}", db));
Debug.WriteLine(string.Format("c={0:F10}", dc));
Debug.WriteLine(string.Format("d={0:F10}", dd));

give the result:
a=100000000001.0000000000
b=100000000000.9990000000
c=100000000000.9940000000
d=1000000000.9999400000

The problem is that a is being rounded to 100000000001 in formatting.

Stepping through the code, da is correctly reported by the debugger as
100000000000.99994

Is this a limitation of string.Format() ? Is there an alterative way
to output a double without rounding?

Boz
Nov 16 '05 #1
  • viewed: 12129
Share:
3 Replies


if you are able to use (or convert) to a decimal type, it will work

this code:

decimal d = 100000000000.99994M;
System.Diagnostics.Trace.WriteLine(d.ToString());

output this
100000000000.99994

-James
"Boz" wrote:
Hi,

I am trying to use string.Format() to output the value of a double.

double da = 100000000000.99994;
double db = 100000000000.9994;
double dc = 100000000000.994;
double dd = 1000000000.99994;

Debug.WriteLine(string.Format("a={0:F10}", da));
Debug.WriteLine(string.Format("b={0:F10}", db));
Debug.WriteLine(string.Format("c={0:F10}", dc));
Debug.WriteLine(string.Format("d={0:F10}", dd));

give the result:
a=100000000001.0000000000
b=100000000000.9990000000
c=100000000000.9940000000
d=1000000000.9999400000

The problem is that a is being rounded to 100000000001 in formatting.

Stepping through the code, da is correctly reported by the debugger as
100000000000.99994

Is this a limitation of string.Format() ? Is there an alterative way
to output a double without rounding?

Boz

Nov 16 '05 #2
Well, I guess it shouldn't do it, but nonetheless it does. If you need an
exact representation of the number, use the "R" (round-trip) format. It
guarantees that parsing the string output yields exactly the same result as
the original number.

Maybe the rounding occurs because the double is guaranteed to have only 15
significant digits. In your example, event the 16th one is a 9, so the
rounding comes into place.

Jon's DecimalConverter shows exact representations of the numbers as
follows:

100000000000.99993896484375
100000000000.9994049072265625
100000000000.9940032958984375
1000000000.99994003772735595703125

As you see, the first one is already malformed at the end. The only
difference is that the debugger probably tries to recover one more
significant digit than the double.ToString() method.

HTH,
Stefan

"Boz" <jj********@yahoo.com> wrote in message
news:32**************************@posting.google.c om...
Hi,

I am trying to use string.Format() to output the value of a double.

double da = 100000000000.99994;
double db = 100000000000.9994;
double dc = 100000000000.994;
double dd = 1000000000.99994;

Debug.WriteLine(string.Format("a={0:F10}", da));
Debug.WriteLine(string.Format("b={0:F10}", db));
Debug.WriteLine(string.Format("c={0:F10}", dc));
Debug.WriteLine(string.Format("d={0:F10}", dd));

give the result:
a=100000000001.0000000000
b=100000000000.9990000000
c=100000000000.9940000000
d=1000000000.9999400000

The problem is that a is being rounded to 100000000001 in formatting.

Stepping through the code, da is correctly reported by the debugger as
100000000000.99994

Is this a limitation of string.Format() ? Is there an alterative way
to output a double without rounding?

Boz

Nov 16 '05 #3
Stefan Simek <si********@kascomp.blah.sk> wrote:
Well, I guess it shouldn't do it, but nonetheless it does. If you need an
exact representation of the number, use the "R" (round-trip) format.
Just a small note of caution - round-trip *doesn't* give the exact
decimal representation.
It guarantees that parsing the string output yields exactly the same
result as the original number.


That it does.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

9 posts views Thread by tym | last post: by
4 posts views Thread by Matt | last post: by
9 posts views Thread by Alex | last post: by
15 posts views Thread by Peter Afonin | last post: by
7 posts views Thread by Joe | last post: by
5 posts views Thread by Jason | last post: by
reply views Thread by jack112 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.