小数点以下の数値を丸める方法は、場合により異なるため関数を作成する必要があります。
例えば、切り捨て1.5なら1にして-1.5なら-1にする関数は標準ではありません。
小数点以下を切り捨てする
Public Shared Overloads Function RoundDown(ByVal val As Double) As Double If 0.0 < val Then return Math.Floor(val) Else return Math.Ceiling(val) End If End Function Public Shared Overloads Function RoundDown(ByVal val As Decimal) As Decimal If 0.0 < val Then return Math.Floor(val) Else return Math.Ceiling(val) End If End Function
小数点以下を切り上する
Public Shared Overloads Function RoundUp(ByVal val As Double) As Double If 0.0 < val Then return Math.Ceiling(val) Else return Math.Floor(val) End If End Function Public Shared Overloads Function RoundUp(ByVal val As Decimal) As Decimal If 0.0 < val Then return Math.Ceiling(val) Else return Math.Floor(val) End If End Function
小数点以下を四捨五入する
Public Shared Overloads Function SimpleRound(ByVal val As Double) As Double Return Math.Round(val, MidpointRounding.AwayFromZero) End Function Public Shared Overloads Function SimpleRound(ByVal val As Decimal) As Decimal Return Math.Round(val, MidpointRounding.AwayFromZero) End Function
少数桁を指定しての丸め処理
'少数桁を指定して小数点以下を切り捨て Public Shared Overloads Function RoundDown(ByVal val As Double, digit As short) As Double Dim d As double = Math.Pow(10.0, digit) return ((RoundDown(val * d) / d)) End Function '少数桁を指定して小数点以下を切り上 Public Shared Overloads Function RoundUp(ByVal val As Double, digit As short) As Double Dim d As double = Math.Pow(10.0, digit) return ((RoundUp(val * d) / d)) End Function '少数桁を指定して小数点以下を四捨五入 Public Shared Overloads Function SimpleRound(ByVal val As Double, digit As short) As Double Dim d As double = Math.Pow(10.0, digit) return ((SimpleRound(val * d) / d)) End Function