2012年4月10日 星期二

VBA / 無條件進位、四捨五入

Round ()
內建

Ceiling ()
 Public Function Ceiling(ByVal X As Double, Optional ByVal Factor As Double = 1) As Double
    ' X is the value you want to round
    ' is the multiple to which you want to round
    Ceiling = (Int(X / Factor) - (X / Factor - Int(X / Factor) > 0)) * Factor
End Function


Floor ()
 Public Function Floor(ByVal X As Double, Optional ByVal Factor As Double = 1) As Double
    ' X is the value you want to round
    ' is the multiple to which you want to round
    Floor = Int(X / Factor) * Factor
End Function




Reference :
How do I simulate Excel's Ceiling/Floor Functions in VBA?


沒有留言:

張貼留言