Click here to Skip to main content
11,505,486 members (70,521 online)
Click here to Skip to main content

MidpointRounding.AwayFromZero functionality in .Net Compact Framework

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

Share

About the Author

Dave5709

United States United States
No Biography provided

Comments and Discussions

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

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150520.1 | Last Updated 14 May 2010
Article Copyright 2010 by Dave5709
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid