Parsing scientific format String to double

  • Thread starter Thread starter K Viltersten
  • Start date Start date
K

K Viltersten

I've got errors when parsing the string
"1.234e+002" to a double. I used the
invariant culture to endure that the
comma is not an issue and i pucked the
styles of the number to be float, to
ensure that scientific format is OK.

double dbl = double.Parse(
"1,34e+002",
NumberStyles.Float,
CultureInfo.InvariantCulture);

Still, i get errors. The error message
is rather undesciptive, telling me only
that the format of the string is wrong.

Of course, i also tried the following,
just to make sure it's not THAT simple.

double dbl = double.Parse(
"1.34e+002",
NumberStyles.Float);

Still, to no avail. What exactly is
wrong here? How do i remedy it?
 
I've got errors when parsing the string "1.234e+002"
Here, you use a comma separator, even though
you ask for the InvariantCulture (which uses a
period). This will never work.


Here you use a period separator, even though
you haven't asked for the InvariantCulture.

How did i ask for InvariantCulture? Is it default
value if nothing else is specified? In that case
the first example SHOULD work.

In fact, i've done some testing and only the
first proof below works. The other three fail
and i don't quite see why. I would expect two
of them to work and two to fail (due to the
collision between culture and separator).

double proof1 = double.Parse(
"12,34e+002", NumberStyles.Float);

double proof2 = double.Parse(
"12,34e+002", NumberStyles.Float, CultureInfo.InvariantCulture);

double proof3 = double.Parse(
"12.34e+002", NumberStyles.Float);

double proof4 = double.Parse(
"12,34e+002", NumberStyles.Float, CultureInfo.InvariantCulture);

Thanks for clarifications!
 
I've got errors when parsing the string "1.234e+002" to a double.
I don't understand the question. I wrote "even though you HAVEN'T asked
for the InvariantCulture".

Serously, friend, me neither. I can't explain how i missed
the negation in your sentence. I've read it carefully many
times over because i know you know your stuff. I guess
it was one of those "d'oh"-days. I really appreciate your
patience. Thanks.
You only have three different examples...

Exactly. I can't explain this either. Too tired or something.
Brains bye bye for the evening. :)
 
Back
Top