65.9K
CodeProject is changing. Read more.
Home

MidpointRounding.AwayFromZero functionality in .Net Compact Framework

starIconemptyStarIconemptyStarIconemptyStarIconemptyStarIcon

1.00/5 (1 vote)

Apr 14, 2010

CPOL
viewsIcon

23451

The Math.Round function in the Compact Framework only allows for ToEven behavior, not the AwayFromZero functionality as listed here:http://msdn.microsoft.com/en-us/library/system.midpointrounding.aspx[^]This code will allow you to emulate the MidpointRounding.AwayFromZero functionality of...

The Math.Round function in the Compact Framework only allows for ToEven behavior, not the AwayFromZero functionality as listed here:
http://msdn.microsoft.com/en-us/library/system.midpointrounding.aspx[^]
 
This code will allow you to emulate the MidpointRounding.AwayFromZero functionality of the full .NET Framework.
 
Public Function RoundAwayFromZero(ByVal startValue As Decimal, ByVal digits As Integer) As Decimal
    Dim decimalValue As Integer
    startValue *= CDec(10 ^ (digits + 1))
    decimalValue = CInt(Decimal.Floor(startValue) - Decimal.Floor(startValue / 10) * 10)
    startValue = Decimal.Floor(startValue / 10)
    If decimalValue >= 5 Then
      startValue += 1
    End If
    startValue /= CDec(10 ^ (digits))
    Return startValue
End Function