vldmrrdjcc
vldmrrdjcc

Reputation: 2132

Convert string representation of numbers less than 1 to double

Convert.ToDouble Method (String) converts the specified String representation of a number to an equivalent double-precision floating point number.

I have this line of code: double d = Convert.ToDouble("0.3");

and it gives me 3.0 instead of 0.3. For numbers greater than 1 it works as expected. Why?

Upvotes: 0

Views: 1453

Answers (3)

marto
marto

Reputation: 4180

What are the current Culture settings? It's probably because in the culture settings you are using the '.' is not the decimal delimiter!

Upvotes: 0

BrokenGlass
BrokenGlass

Reputation: 160962

Try this:

NumberFormatInfo provider = new NumberFormatInfo();
provider.NumberDecimalSeparator = ".";
double d = Convert.ToDouble("0.3", provider);

Upvotes: 1

CodesInChaos
CodesInChaos

Reputation: 108830

Most likely a locale problem. There are cultures where . is not the decimal separator.

Try double.Parse("0.3",CultureInfo.InvariantCulture)

Upvotes: 6

Related Questions