Click here to Skip to main content
15,885,757 members
Articles / Mobile Apps / Windows Mobile
Tip/Trick

MidpointRounding.AwayFromZero functionality in .Net Compact Framework

Rate me:
Please Sign up or sign in to vote.
1.00/5 (1 vote)
14 May 2010CPOL 22.9K   1   3
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.
 
VB
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
SuggestionC# version Pin
Oskar Emil4-May-15 3:43
Oskar Emil4-May-15 3:43 
QuestionAwayFromZero Pin
Son Nguyen Thanh5-May-13 16:39
Son Nguyen Thanh5-May-13 16:39 
QuestionAwayFromZero in another way Pin
pawel8925-Jun-12 1:03
pawel8925-Jun-12 1:03 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.