Created
June 4, 2015 15:59
-
-
Save ooltcloud/7ba0ebf1a86f8d2c638f to your computer and use it in GitHub Desktop.
丸め処理 (小数点以下 n 位で丸める)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' <summary> | |
''' 丸め | |
''' </summary> | |
''' <remarks> | |
''' 参考) | |
''' http://ja.wikipedia.org/wiki/%E7%AB%AF%E6%95%B0%E5%87%A6%E7%90%86#.E4.B8.B8.E3.82.81.E3.81.AE.E7.A8.AE.E9.A1.9E | |
''' http://rurema.clear-code.com/2.1.0/method/BigDecimal/s/mode.html | |
''' </remarks> | |
Public Class Rounding | |
''' <summary> | |
''' 切り上げ (指定値を下回らない最小値 / RM: Rounding toward Minus infinity) | |
''' </summary> | |
Public Shared Function Ceiling(value As Double, digits As Integer) | |
Dim shift = 10 ^ digits | |
Return Math.Ceiling(value * shift) / shift | |
End Function | |
''' <summary> | |
''' 切り捨て (指定値を上回らない最大値 / RP: Rounding toward Plus infinity) | |
''' </summary> | |
Public Shared Function Floor(value As Double, digits As Integer) | |
Dim shift = 10 ^ digits | |
Return Math.Floor(value * shift) / shift | |
End Function | |
''' <summary> | |
''' 切り捨て (0に近い側 / RZ: Rounding toward Zero) | |
''' </summary> | |
Public Shared Function Down(value As Double, digits As Integer) | |
Dim shift = 10 ^ digits | |
Return Math.Truncate(value * shift) / shift | |
End Function | |
''' <summary> | |
''' 切り上げ (0に遠い側 / RI: Rounding toward Infinity) | |
''' </summary> | |
Public Shared Function Up(value As Double, digits As Integer) | |
Dim shift = 10 ^ digits | |
Dim v = value * shift | |
Return If(v < 0, Math.Floor(v), Math.Ceiling(v)) / shift | |
End Function | |
''' <summary> | |
''' 四捨五入 (R: Round) | |
''' </summary> | |
Public Shared Function HalfUp(value As Double, digits As Integer) | |
Return Math.Round(value, digits, MidpointRounding.AwayFromZero) | |
End Function | |
''' <summary> | |
''' 偶数丸め (RN: Round to the Nearest even) | |
''' </summary> | |
Public Shared Function HalfEven(value As Double, digits As Integer) | |
Return Math.Round(value, digits) | |
End Function | |
End Class |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment