float.Parse返回不正确的值 [英] float.Parse returns incorrect value

查看:155
本文介绍了float.Parse返回不正确的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个字符串X 值为 2.26

当我分析它使用 float.Parse(X) ..它返回 2.2599999904632568 。为什么这样?以及如何克服呢?

when I parse it using float.Parse(X) ..it returns 2.2599999904632568. Why so? And how to overcome this ?

但如果相反,我用 double.Parse(X)返回的精确值,即 2.26

But if instead I use double.Parse(X) it returns the exact value, i.e. 2.26.

编辑: code

float.Parse(dgvItemSelection[Quantity.Index, e.RowIndex].Value.ToString());

感谢您的帮助

Thanks for help

推荐答案

这是由于浮点数的precision限制。他们不能再present无限precise价值观,往往采取近似值。如果您需要高度precise号码,你应该使用十进制代替。

This is due to limitations in the precision of floating point numbers. They can't represent infinitely precise values and often resort to approximate values. If you need highly precise numbers you should be using Decimal instead.

有关于这个问题,你应该看一看了大量文学作品。我最喜欢的资源是以下

There is a considerable amount of literature on this subject that you should take a look at. My favorite resource is the following

这篇关于float.Parse返回不正确的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆