2014-03-31 83 views
2

當我調用這個函數像這樣Rounded(50.080,3)我得到「50.08」如何強制保持尾隨零?圓號但保留精確數字

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

你的意思是零尾隨做到這一點? – Blorgbeard

+2

你是不是指尾隨零?這是一個演示問題,實際上不應該是舍入函數的關注。 – DeanOC

回答

3
Dim RoundedString As String = FormatNumber(Rounded, Decimals, , , TriState.False) 

應該爲你

+0

這不起作用,因爲它將「1542.800」轉換爲「1,542.800」 – XK8ER

+0

對不起。我編輯它,現在應該修復它。 – Schickmeister

1

請,爲自己的健康,打開Option StrictOption 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")爲小數點後三位,和完全放棄Rounded