Vb.net 整数,但保留精确数字

Vb.net 整数,但保留精确数字,vb.net,formatting,significant-digits,Vb.net,Formatting,Significant Digits,当我这样调用这个函数时,取整(50.080,3),我得到了“50.08”,我怎么能强制保持后面的零呢 Public Function Rounded(ByVal Number, ByVal Decimals) Rounded = Int(Number * 10 ^ Decimals + 1 / 2) / 10 ^ Decimals End Function 您可以使用decimal.Round(值,2,中点舍入.AwayFromZero), 查看更多详细信息您可以使用十进制.Round

当我这样调用这个函数时,取整(50.080,3),我得到了“50.08”,我怎么能强制保持后面的零呢

Public Function Rounded(ByVal Number, ByVal Decimals)
    Rounded = Int(Number * 10 ^ Decimals + 1 / 2) / 10 ^ Decimals
End Function

您可以使用
decimal.Round(值,2,中点舍入.AwayFromZero)

查看更多详细信息您可以使用
十进制.Round(值,2,中点舍入.AwayFromZero)

查看更多详细信息

为了您自己的健康,请打开
Option Strict
Option Explicit
,除非您确实想停止使用返回变量,并删除旧版VB6函数(如
Int

然后,您可能可以使用如下字符串格式来破解某些内容:

Public Function Rounded(number As Decimal, decimals As Integer) As String
    Return number.ToString("0." & New String("0"c, decimals))
End Function

如果可以的话,通常最好自己使用格式字符串,如
num.ToString(“0.000”)
中的小数点后三位,然后完全舍入
四舍五入的

为了您自己的健康,请打开
选项Strict
选项Explicit
,除非确实需要,否则请停止使用返回变量,并删除旧的VB6函数(如
Int

Dim RoundedString As String = FormatNumber(Rounded, Decimals, , , TriState.False)
然后,您可能可以使用如下字符串格式来破解某些内容:

Public Function Rounded(number As Decimal, decimals As Integer) As String
    Return number.ToString("0." & New String("0"c, decimals))
End Function
如果可以的话,通常最好自己使用格式字符串,如
num.ToString(“0.000”)
中的小数点后三位,然后完全舍入
Rounded

Dim RoundedString As String = FormatNumber(Rounded, Decimals, , , TriState.False)
我应该为你做


应该为你做吗

你是说后面的零吗?你不是说后面的零吗?这是一个表示问题,真的不应该是舍入函数所关心的。你是指尾随的零吗?你不是指尾随的零吗?这是一个表示问题,不应该是舍入函数所关心的。这不起作用,因为它将“1542.800”转换为“1542.800”。这不起作用,因为它将“1542.800”转换为“1542.800”