467,925 Members | 1,859 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

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

How do I escape from the exponential format when writing Double.ToString()

I'm trying to print a Double in straight decimal form, not exponential. I can't seem to accomplish this. This seems like it should
be simple enough. The output I'm looking for is "0.00001", not "1E-05".

I'm obviously baffled by the NumberFormatInfo class, I can't seem to make it do what I want. And, I've tried the following, all of
which have failed to modify the original exponential form. The commented out "D7" statement throws a FormatException. Output is as
follows:

default=1E-05
G7=1E-05
N7=1E-05
F7=1E-05

Here's the code snippet:

double f = 0.00001D;

Console.WriteLine("default=" + f.ToString());
Console.WriteLine("G7="+Double.Parse(f.ToString("G 7")));
Console.WriteLine("N7="+Double.Parse(f.ToString("N 7")));
Console.WriteLine("F7="+Double.Parse(f.ToString("F 7")));
//Console.WriteLine("D7="+Double.Parse(((double)f).T oString("D7")));
Nov 16 '05 #1
  • viewed: 8804
Share:
2 Replies
Rod --

The statement

Console.WriteLine("F7="+Double.Parse(f.ToString("F 7")));

is equivalent to the following code:

string s1 = f.ToString("F7");
double f1 = Double.Parse(s);
string s2 = f1.ToString();
Console.WriteLine("F7=" + s2);

So, you're converting f to a string ("0.0000100") back to a double and then
back to a string again, but this time using the default string formatting
(i.e. exponential). Instead, just use:

Console.WriteLine("F7="+f.ToString("F7"));

or

Console.WriteLine("F7={0:F7}", f);

Ken
"Rod Brick" <ro*@hillcast.com> wrote in message
news:eW**************@TK2MSFTNGP09.phx.gbl...
I'm trying to print a Double in straight decimal form, not exponential. I can't seem to accomplish this. This seems like it should be simple enough. The output I'm looking for is "0.00001", not "1E-05".

I'm obviously baffled by the NumberFormatInfo class, I can't seem to make it do what I want. And, I've tried the following, all of which have failed to modify the original exponential form. The commented out "D7" statement throws a FormatException. Output is as follows:

default=1E-05
G7=1E-05
N7=1E-05
F7=1E-05

Here's the code snippet:

double f = 0.00001D;

Console.WriteLine("default=" + f.ToString());
Console.WriteLine("G7="+Double.Parse(f.ToString("G 7")));
Console.WriteLine("N7="+Double.Parse(f.ToString("N 7")));
Console.WriteLine("F7="+Double.Parse(f.ToString("F 7")));
//Console.WriteLine("D7="+Double.Parse(((double)f).T oString("D7")));

Nov 16 '05 #2
Doh!

Thanks for the insight.
Nov 16 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by newsgroupie | last post: by
4 posts views Thread by Abraham Luna | last post: by
10 posts views Thread by ruca | last post: by
7 posts views Thread by Axel Dahmen | last post: by
9 posts views Thread by Nico VanHaaster | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.