VB.Net 中的 Math.Round() 有什么问题?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14835001/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is wrong with Math.Round() in VB.Net?
提问by Mtok
I have encountered a weird case in Math.Round function in VB.Net
我在 VB.Net 中的 Math.Round 函数中遇到了一个奇怪的情况
Math.Round((32.625), 2)
Result : 32.62
结果:32.62
Math.Round((32.635), 2)
Result : 32.64
结果:32.64
I need 32.63but the function is working in different logic in these cases.
我需要32.63,但在这些情况下,该函数以不同的逻辑运行。
I can get the decimal part and make what I want doing something on it. But isn't this too weird, one is rounding to higher, one is rounding to lower.
我可以得到小数部分并在上面做我想做的事情。但这不是太奇怪了,一个是四舍五入到更高,一个是四舍五入到更低。
So how can I get 32.63from 32.625without messing with decimal part ? (as the natural logic of Maths)
那么如何在不弄乱小数部分的情况下从32.625得到32.63呢?(作为数学的自然逻辑)
回答by Steven Doggart
Math.Round
uses banker's roundingby default. You can change that by specifying a different MidPointRounding
option. From the MSDN:
Math.Round
默认情况下使用银行家的舍入。您可以通过指定不同的MidPointRounding
选项来更改它。从MSDN:
Rounding away from zero
从零舍入
Midpoint values are rounded to the next number away from zero. For example, 3.75 rounds to 3.8, 3.85 rounds to 3.9, -3.75 rounds to -3.8, and -3.85 rounds to -3.9. This form of rounding is represented by the MidpointRounding.AwayFromZero enumeration member. Rounding away from zero is the most widely known form of rounding.
中点值四舍五入到远离零的下一个数字。例如,3.75 轮到 3.8,3.85 轮到 3.9,-3.75 轮到 -3.8,-3.85 轮到 -3.9。这种形式的舍入由 MidpointRounding.AwayFromZero 枚举成员表示。从零舍入是最广为人知的舍入形式。
Rounding to nearest, or banker's rounding
四舍五入到最接近的,或银行家的四舍五入
Midpoint values are rounded to the nearest even number. For example, both 3.75 and 3.85 round to 3.8, and both -3.75 and -3.85 round to -3.8. This form of rounding is represented by the MidpointRounding.ToEven enumeration member.
Rounding to nearest is the standard form of rounding used in financial and statistical operations. It conforms to IEEE Standard 754, section 4. When used in multiple rounding operations, it reduces the rounding error that is caused by consistently rounding midpoint values in a single direction. In some cases, this rounding error can be significant.
中点值四舍五入到最接近的偶数。例如,3.75 和 3.85 都舍入到 3.8,-3.75 和 -3.85 都舍入到 -3.8。这种形式的舍入由 MidpointRounding.ToEven 枚举成员表示。
四舍五入是财务和统计操作中使用的标准四舍五入形式。它符合 IEEE 标准 754 第 4 节。在多次舍入操作中使用时,它减少了由在单个方向上一致舍入中点值引起的舍入误差。在某些情况下,这种舍入误差可能很大。
So, what you want is:
所以,你想要的是:
Math.Round(32.625, 2, MidpointRounding.AwayFromZero)
Math.Round(32.635, 2, MidpointRounding.AwayFromZero)
As others have mentioned, if precision is important, you should be using Decimal
variables rather than floating point types. For instance:
正如其他人提到的,如果精度很重要,您应该使用Decimal
变量而不是浮点类型。例如:
Math.Round(32.625D, 2, MidpointRounding.AwayFromZero)
Math.Round(32.635D, 2, MidpointRounding.AwayFromZero)
回答by Mr47
Try this (from memory):
试试这个(从记忆中):
Math.Round((32.635), 2, MidPointRounding.AwayFromZero)
Math.Round((32.635), 2, MidPointRounding.AwayFromZero)
回答by Richard Sites
Hers a quick function you can add to simplify your life and make it so you don't have to type so much all the time.
她是一个快速功能,您可以添加它来简化您的生活,并使您不必一直打字。
Private Function roundd(dec As Decimal)
Dim d As Decimal = dec
Dim r As Decimal = Math.Ceiling(d * 100D) / 100D
Return r
End Function
Add this to your application then use the function
将此添加到您的应用程序然后使用该功能
roundd(3.624)
or whatever you need.
或任何你需要的。
to display the result - example
显示结果 - 示例
msgbox(roundd(3.625))
This will display a messagebox with 3.63
这将显示一个带有 3.63 的消息框
Textbox1.text = roundd(3.625)
this will set textbox1.text - 3.63 etc. etc. So if you need to round more then one number, it won't be so tedious and you can save alot of typing.
这将设置 textbox1.text - 3.63 等。因此,如果您需要舍入多于一个数字,则不会那么乏味,而且您可以节省大量输入。
Hope this helps.
希望这可以帮助。
回答by Richard Sites
Try this.
尝试这个。
Dim d As Decimal = 3.625
Dim r As Decimal = Math.Ceiling(d * 100D) / 100D
MsgBox(r)
This should do what you want.
这应该做你想做的。
回答by user151019
You can't using floats which is what numbers like 32.625 is treated as in VB.Net. (There is also the issue of Banker's rounding as mention by @StevenDoggart - you are probably going to have to deal with both issues.)
您不能使用浮点数,这就是 32.625 之类的数字在 VB.Net 中的处理方式。(还有@StevenDoggart 提到的银行家四舍五入问题-您可能不得不处理这两个问题。)
The issue is that the number stored is not exactly what is entered because these numbers do not into a fixed binary representation e.g. 32.625 is stored as 32.62499997 and 32.635 as 32.63500001.
The only way to be exact is to store the numbers as the type Decimal
问题是存储的数字与输入的数字不完全相同,因为这些数字不是固定的二进制表示,例如 32.625 存储为 32.62499997,32.635 存储为 32.63500001。
唯一准确的方法是将数字存储为 Decimal 类型
DIM num as Decimal
num = ToDecimal("32.625")