Click here to Skip to main content
11,647,827 members (64,105 online)
Click here to Skip to main content

MidpointRounding.AwayFromZero functionality in .Net Compact Framework

, 14 May 2010 CPOL 11K 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:[^]This code will allow you to emulate the MidpointRounding.AwayFromZero functionality of...
The Math.Roundfunction in the Compact Framework only allows for ToEvenbehavior, not the AwayFromZerofunctionality as listed here:[^]
This code will allow you to emulate the MidpointRounding.AwayFromZerofunctionality 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


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

United States United States
No Biography provided

You may also be interested in...

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
Web01 | 2.8.150804.4 | Last Updated 14 May 2010
Article Copyright 2010 by Dave5709
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid