Click here to Skip to main content
Click here to Skip to main content

MidpointRounding.AwayFromZero functionality in .Net Compact Framework

By , 14 May 2010
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 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

Comments and Discussions

QuestionAwayFromZero PinmemberSon Nguyen Thanh5-May-13 16:39 
QuestionAwayFromZero in another way Pinmemberpawel8925-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 | Mobile
Web01 | 2.8.140415.2 | Last Updated 14 May 2010
Article Copyright 2010 by Dave5709
Everything else Copyright © CodeProject, 1999-2014
Terms of Use
Layout: fixed | fluid