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

  • Thread starter Thread starter Rod Brick
  • Start date Start date
R

Rod Brick

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("G7")));
Console.WriteLine("N7="+Double.Parse(f.ToString("N7")));
Console.WriteLine("F7="+Double.Parse(f.ToString("F7")));
//Console.WriteLine("D7="+Double.Parse(((double)f).ToString("D7")));
 
Rod --

The statement

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

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 said:
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
 
Back
Top