在VB.NET中进行四舍五入的语法

leo*_*ita 14 vb.net

将小数点后两位数四舍五入的语法是什么?

示例:2.566666 - > 2.57

Guf*_*ffa 13

如果要进行常规舍入,可以使用该Math.Round方法.如果您特别想要向上舍入,则使用以下Math.Ceiling方法:

Dim d As Decimal = 2.566666
Dim r As Decimal = Math.Ceiling(d * 100D) / 100D
Run Code Online (Sandbox Code Playgroud)


小智 12

我是这样做的:

Private Function RoundUp(value As Double, decimals As Integer) As Double

    Return Math.Ceiling(value * (10 ^ decimals)) / (10 ^ decimals)

End Function
Run Code Online (Sandbox Code Playgroud)


Sco*_*vey 7

Math.Round就是你要找的东西.如果您不熟悉 .NET中的四舍五入 - 您还应该查看AwayFromZeroToEven四舍五入之间的区别.默认的ToEven有时会让人大吃一惊.

dim result = Math.Round(2.56666666, 2)
Run Code Online (Sandbox Code Playgroud)


Nic*_*ver 3

System.Math具体来说,您可以Math.Round()像这样使用:

Math.Round(2.566666, 2)
Run Code Online (Sandbox Code Playgroud)