Parsing double precision numbers

M

marathoner

After reading the string representation of a double precision
number, -0.417597000000000D+06, from a text file, I am unable to convert it
to a double precision number. Here is the code I am using:

string[] result = block.Split(charSeparators,
StringSplitOptions.RemoveEmptyEntries);
double temp1 = double.Parse(result[0]);

After the last statement, I get an unhandled exception.

Marathoner
 
W

Willy Denoyette [MVP]

marathoner said:
After reading the string representation of a double precision
number, -0.417597000000000D+06, from a text file, I am unable to convert
it to a double precision number. Here is the code I am using:

string[] result = block.Split(charSeparators,
StringSplitOptions.RemoveEmptyEntries);
double temp1 = double.Parse(result[0]);

After the last statement, I get an unhandled exception.

Marathoner


-0.417597000000000D+06 is invalid (the D should be an e or E to be valid),
where did you get this from?

Willy.
 
T

Tom Porterfield

marathoner said:
After reading the string representation of a double precision
number, -0.417597000000000D+06, from a text file, I am unable to convert it
to a double precision number. Here is the code I am using:

string[] result = block.Split(charSeparators,
StringSplitOptions.RemoveEmptyEntries);
double temp1 = double.Parse(result[0]);

After the last statement, I get an unhandled exception.

What is the D in your number? If that is supposed to mean exponent, the
correct representation is e (or E).
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,


The D is incorrect, it should be an E.

Where are you reading these numbers from?

Anyway, use String.Replace
double temp1 = double.Parse(result[0].Replace("D","E"));
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top