在 VB.NET 中四舍五入的语法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2862274/
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
Syntax for rounding up in VB.NET
提问by leonita
What is the syntax to round up a decimal leaving two digits after the decimal point?
舍入小数点后保留两位数的语法是什么?
Example: 2.566666 -> 2.57
示例:2.566666 -> 2.57
回答by Guffa
If you want regular rounding, you can just use the Math.Round
method. If you specifially want to round upwards, you use the Math.Ceiling
method:
如果您想要定期舍入,您可以使用该Math.Round
方法。如果您特别想向上舍入,则使用以下Math.Ceiling
方法:
Dim d As Decimal = 2.566666
Dim r As Decimal = Math.Ceiling(d * 100D) / 100D
回答by Pierre Gauvin
Here is how I do it:
这是我如何做到的:
Private Function RoundUp(value As Double, decimals As Integer) As Double
Return Math.Ceiling(value * (10 ^ decimals)) / (10 ^ decimals)
End Function
回答by Scott Ivey
Math.Roundis what you're looking for. If you're new to rounding in .NET - you should also look up the differencebetween AwayFromZeroand ToEvenrounding. The default of ToEven can sometime take people by surprise.
Math.Round正是您要找的。如果您不熟悉.NET 中的舍入 - 您还应该查看AwayFromZero和ToEven舍入之间的区别。ToEven 的默认设置有时会让人们大吃一惊。
dim result = Math.Round(2.56666666, 2)
回答by Nick Craver
You can use System.Math
, specifically Math.Round()
, like this:
您可以使用System.Math
,具体来说Math.Round()
,像这样:
Math.Round(2.566666, 2)
回答by Joel Coehoorn
Math.Round()
, as suggested by others, is probably what you want. But the text of your question specifically asked how to "roundup"[sic]. If you always need to round up, regarless of actual value (ie: 2.561111 would still go to 2.57), you can do this:
Math.Round()
,正如其他人所建议的那样,可能是您想要的。但是你的问题的文字特别询问了如何“综述”[原文如此]。如果你总是需要四舍五入,不管实际值如何(即:2.561111 仍然会去 2.57),你可以这样做:
Math.Ceiling(d * 100)/100D
回答by Hunter Wright
The basic function for rounding up is Math.Ceiling(d), but the asker specifically wanted to round up after the second decimal place. This would be Math.Ceiling(d * 100) / 100. For example, it may multiply 46.5671 by 100 to get 4656.71, then rounds up to get 4657, then divides by 100 to shift the decimal back 2 places to get 46.57.
四舍五入的基本函数是 Math.Ceiling(d),但提问者特别想在小数点后第二位四舍五入。这将是 Math.Ceiling(d * 100) / 100。例如,它可以将 46.5671 乘以 100 得到 4656.71,然后四舍五入得到 4657,然后除以 100 将小数点后移两位得到 46.57。
回答by user5856137
I used this way:
我用这种方式:
Math.Round(d + 0.49D, 2)
回答by mahendra rajput
Math.Ceiling((14.512555) * 100) / 100
Dot net will give you 14.52
. So, you can use above syntax to round the number up for 2 decimal numbers.
点网会给你14.52
。因此,您可以使用上述语法将数字向上舍入为 2 个十进制数字。
回答by Jeremy Pullicino
I do not understand why people are recommending the incorrect code below:
我不明白为什么人们推荐以下不正确的代码:
Dim r As Decimal = Math.Ceiling(d * 100D) / 100D
The correct code to round up should look like this:
正确的四舍五入代码应如下所示:
Dim r As Double = Math.Ceiling(d)
- Math.Ceiling works with data type Double (not Decimal).
- The * 100D / 100D is incorrect will break your results for larger numbers.
- Math.Ceiling documentation is found here: http://msdn.microsoft.com/en-us/library/zx4t0t48.aspx
- Math.Ceiling 使用数据类型 Double(不是 Decimal)。
- * 100D / 100D 不正确会破坏较大数字的结果。
- Math.Ceiling 文档可以在这里找到:http: //msdn.microsoft.com/en-us/library/zx4t0t48.aspx