加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 第1,7346至1,74轮

发布时间:2020-12-16 01:49:47 所属栏目:百科 来源:网络整理
导读:我使用Math.Round(1.7346,2,AwayFromZero)应该给我1,74 – 但它实际上给了我1.73. 我知道是因为这个: “Because of the loss of precision that can result from representing decimal values as floating-point numbers or performing arithmetic operatio
我使用Math.Round(1.7346,2,AwayFromZero)应该给我1,74 – 但它实际上给了我1.73.

我知道是因为这个:

“Because of the loss of precision that can result from representing
decimal values as floating-point numbers or performing arithmetic
operations on floating-point values,in some cases the Round(Double,
Int32,MidpointRounding) method may not appear to round midpoint
values as specified by the mode parameter. This is illustrated in the
following example,where 2.135 is rounded to 2.13 instead of 2.14.
This occurs because internally the method multiplies value by 10 *
digits,and the multiplication operation in this case suffers from a
loss of precision.”

但是我应该使用哪种.NET方法来进行正确的舍入?

解决方法

1.7346应该四舍五入到1.73,即使是 AwayFromZero,因为这只会改变“当一个数字介于另外两个之间时”的舍入行为(例如,如果它是1.735).

您可以使用获得所需的行为

Math.Round(Math.Round(1.7346,3,AwayFromZero),AwayFromZero)

(但不要认为这不仅仅是一个黑客).

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读