keerthee
keerthee

Reputation: 880

floating precision lost in float.Parse

float s = float.Parse("10499.9705314636");

Now s has the value 10499.97.

Is there a way to save all the precision digits?

Upvotes: 2

Views: 6622

Answers (3)

joelmdev
joelmdev

Reputation: 11773

You may want to look into the difference between float, double, and decimal. Pay special attention to the difference between a binary floating point type, and a decimal floating point type. Decimal data type is likely what you're looking for here, as float doesn't have enough significant digits to store the number you're trying to parse accurately.

Upvotes: 7

Patrick Hofman
Patrick Hofman

Reputation: 156948

Try using a data type with a higher precision than float, such as double. The remaining digits of your value are truncated due to the size of the data type float:

double s = double.Parse("10499.9705314636");
Console.WriteLine(s);

Prints:

10499.9705314636

Upvotes: 4

HavelTheGreat
HavelTheGreat

Reputation: 3386

You should try and use decimal instead.

Upvotes: 2

Related Questions