floating precision lost in float.Parse

2019-08-09 14:05发布

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

Now s has the value 10499.97.

Is there a way to save all the precision digits?

3条回答
Viruses.
2楼-- · 2019-08-09 14:29

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.

查看更多
乱世女痞
3楼-- · 2019-08-09 14:30

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

查看更多
ゆ 、 Hurt°
4楼-- · 2019-08-09 14:34

You should try and use decimal instead.

查看更多
登录 后发表回答