避免 Convert.tosingle 方法中的舍入

发布于 2024-09-15 09:09:15 字数 173 浏览 3 评论 0原文

考虑一下,

object a =1.123456;
float f = convert.ToSingle(a);

但是当我打印 f 的值时,我得到 1.123455。

它正在四舍五入。 另外问题是我无法更改代码中 float 的数据类型。 请帮忙。

consider ,

object a =1.123456;
float f = convert.ToSingle(a);

But when I print the value of f , I get 1.123455.

It is getting rounded off.
Also the problem is I cant change the data type of float in the code.
Please help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一袭水袖舞倾城 2024-09-22 09:09:16

这样做是因为浮点类型的工作方式。
如果您想要更好的精度(以牺牲一些性能为代价) - 请改用 Double 或 Decimal 类型。

有关浮点为何失去精度的更多信息,请阅读:
http://msdn.microsoft.com/en-我们/库/c151dt3s%28VS.80%29.aspx

This is done because of the way the floating-point type works.
If you want a better precision (in the cost of some performance) - use the Double or Decimal type instead.

For more information about why floating-point loses precision, read:
http://msdn.microsoft.com/en-us/library/c151dt3s%28VS.80%29.aspx

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文