净四舍五入问题

编程入门 行业动态 更新时间:2024-10-23 15:31:13
本文介绍了净四舍五入问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有净内部的四舍五入问题。

I have a rounding issue inside of .Net.

我四舍五入3位数字下降到两位数字,它会引起一些问题,一个数字。

I am rounding a 3 digit number down to two digits and it is causing some problems with one number.

如果我试图轮34.425到小数点后两位,应该是四舍五入至34.43。我现在用的是roundawayfromzero选项,它在程序中,除了这一个至今就职于每个数字。

If I try to round 34.425 to two decimal places it should round it to 34.43. I am using the roundawayfromzero option and it has worked for every number in the program except for this one so far.

在code Math.Round(34.425,2,MidpointRounding.AwayFromZero)但是应该等于34.43,它等于34.42。

The code Math.Round(34.425, 2, MidpointRounding.AwayFromZero) should equal 34.43 however, it equals 34.42.

如果我尝试用任何其他数字,它工作正常。

If I try this with any other number it works fine.

Math.Round(34.435, 2, MidpointRounding.AwayFromZero) = 34.44 Math.Round(34.225, 2, MidpointRounding.AwayFromZero) = 34.23 Math.Round(34.465, 2, MidpointRounding.AwayFromZero) = 34.47

我只是想检查,看看是否有人以前碰到这个问题?

I just wanted to check to see if anyone has run into this problem before?

有关,现在我已经通过数转换为十进制解决了这个问题。我已经改变了code到这一点,它工作正常现在:

For right now I have fixed this problem by converting the number to a decimal. I have changed the code to this and it works fine now:

Math.Round(CDec(34.425), 2, MidpointRounding.AwayFromZero) = 34.43

我只是在寻找为什么我的旧code没有工作的理由。

I am just looking for a reason on why my old code did not work.

感谢您!

更新了code为正确AwayFromZero

Updated the code to the correct AwayFromZero

推荐答案

浮点从来都不是确切的,34.425可能有一个内部的重新presantation 34.4249999999999 ..这将四舍五入至34.42。

Floating point is never exact, 34.425 may have an internal represantation 34.4249999999999.. which will be rounded up to 34.42.

如果你需要精确的再presentation换号使用十进制键入

If you need exact representation for numbers use the decimal type.

更多推荐

净四舍五入问题

本文发布于:2023-10-28 19:37:34,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1537588.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:四舍五入

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!