Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

MidpointRounding.AwayFromZero functionality in .Net Compact Framework

0.00/5 (No votes)
14 May 2010 1  
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

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here