467,077 Members | 997 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

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

Formatting a double.ToString()

CJ
This is a really silly question, but I just can't get it right.

I have a double; the value is 0.00003025. I want to display it exactly
as it stands, but by default it is displayed in scientific notation.
I've tried specifying the format as "G", "R", "F", whatever; none of
them gives me the result I want. The closest is "N", but then I have
to specify the number of digits/decimals that I want to display, and
this may vary - now the value is 0.00003025, next time it may be 0.02
and then I don't want it to display as 0.02000000!

Nov 29 '05 #1
  • viewed: 2342
Share:
2 Replies
CJ wrote:
This is a really silly question, but I just can't get it right.

I have a double; the value is 0.00003025. I want to display it exactly
as it stands, but by default it is displayed in scientific notation.
I've tried specifying the format as "G", "R", "F", whatever; none of
them gives me the result I want. The closest is "N", but then I have
to specify the number of digits/decimals that I want to display, and
this may vary - now the value is 0.00003025, next time it may be 0.02
and then I don't want it to display as 0.02000000!


The first thing you need to be aware of is that you *don't* have a
double with a value of 0.00003025. The closest you can have is a double
with a value of exactly
0.000030250000000000000060498481224691147417615866 28854274749755859375.

Now, given that information, how do you know what you need to display?

I *suspect* that the round-trip format specifier ("R") would be what
you wanted in terms of precision - but that the scientific notation
part is where you get message up.

I'll think about it further, but I hope the above helps a bit...

Jon

Nov 29 '05 #2
CJ

Jon Skeet [C# MVP] wrote:
CJ wrote:
This is a really silly question, but I just can't get it right.

I have a double; the value is 0.00003025. I want to display it exactly
as it stands, but by default it is displayed in scientific notation.
I've tried specifying the format as "G", "R", "F", whatever; none of
them gives me the result I want. The closest is "N", but then I have
to specify the number of digits/decimals that I want to display, and
this may vary - now the value is 0.00003025, next time it may be 0.02
and then I don't want it to display as 0.02000000!


The first thing you need to be aware of is that you *don't* have a
double with a value of 0.00003025. The closest you can have is a double
with a value of exactly
0.000030250000000000000060498481224691147417615866 28854274749755859375.

Now, given that information, how do you know what you need to display?

I *suspect* that the round-trip format specifier ("R") would be what
you wanted in terms of precision - but that the scientific notation
part is where you get message up.

I'll think about it further, but I hope the above helps a bit...

Jon


Yeah, unfortunately I'm well aware of the problems with exact values of
floating point numbers. If I set a watch on the variable, though,
that's the value that it's showing me (it's the result of a
calculation). It's unlikely that there'll ever be such a small value
in the live environment, so I suspect I might just have to live with
the possibility that if there is, it'll be displayed in scientific
notation.

Nov 29 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by BBFrost | last post: by
2 posts views Thread by Homer Simpson | last post: by
4 posts views Thread by ChrisB | last post: by
7 posts views Thread by sujoy kar | last post: by
3 posts views Thread by yuvalif | last post: by
14 posts views Thread by Scott M. | last post: by
1 post views Thread by Lee | last post: by
3 posts views Thread by =?Utf-8?B?RGFycmVsbCBXZXNsZXk=?= | last post: by
13 posts views Thread by cj2 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.