切り捨て・切り上げ・四捨五入

下の例は0に関して対称に丸めます。切り上げに関しては0.9を足して切り捨てをする方法がNifty Forumに紹介されているのを見かけますが間違いですので気をつけてください。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
'切り捨て
Function Floor(x As Double) As Long
Floor = Fix(x)
End Function

'切り上げ
Function Ceil(x As Double) As Long
Ceil = -Sgn(x) * Int(-Sgn(x) * x)
End Function

'四捨五入
Function Round(x As Double) As Long
Round = Fix(x + 0.5 * Sgn(x))
End Function