Currency Rounding In VB.Net and Visual Studio
How to correctly round up to the next higher penny of a currency value in VB.Net or other Visual Studio languages.
Seeing as you've found this page you're probably having fun and games with rounding and decimal places in VB.Net or one of the other Visual Studio languages. You might have found that the Math.Round function rounds to the nearest even number. This has its advantages for some rounding operations, but for me, this is normally NOT what I want to achieve when working with currency values.
The VB6 Currency data type is missing from VB.Net, but you can use the SqlMoney data type instead. Try the following function. It rounds UP to the next higher penny. (Or DOWN to the next lower penny for negative values)
Public Function roundUpToWholePence(ByVal dVal As Decimal) As Decimal Dim m As New SqlTypes.SqlMoney(dVal) Dim HUNDRED As New SqlTypes.SqlMoney(100) Return SqlTypes.SqlMoney.Multiply( _ SqlTypes.SqlMoney.Divide(m, HUNDRED), HUNDRED _ ).ToDecimal End Function
' 0.001 => 0.00
' 0.005 => 0.01
' 0.015 => 0.02
' -0.001 => 0.00
' -0.005 => -0.01
' -0.015 => -0.02
Is that what you were after? I'm sure you can easily convert that to other Visual Studio languages if you need to.