Click here to Skip to main content
Licence 
First Posted 29 Oct 2004
Views 33,923
Bookmarked 9 times

Asymmetric Arithmetic Rounding

By | 29 Oct 2004 | Article
.NET uses Bankers rounding. Want to bring back the rounding they taught you in grade school? This simple bit of code will help!

Introduction

.NET rounding is limited to bankers rounding. I searched far and wide for an alternative. I didn't find one. There may be better ways to do this, but I believe this is pretty lightweight and simple.

The Scoop: 1 - 4: rounds down, 5 - 9: rounds up. Simply add these functions to a class or a form or whatever you are programming, and call them whenever you need to do rounding.

public static float aaRounding(float numToRound, int numOfDec)
{
    return (float)aaRounding((decimal)numToRound, numOfDec);
}

public static double aaRounding(double numToRound, int numOfDec)
{
    return (double)aaRounding((decimal)numToRound, numOfDec);
}
  
public static decimal aaRounding(decimal numToRound, int numOfDec)
{
    if (numOfDec < 0)
    {
        throw new ArgumentException("BetterMath.Rounding:" + 
              " Number of decimal places must be 0 or greater", 
              "numOfDec");
    }

    decimal num = numToRound;

    //Shift the decimal to the right the number 
    //of digits they want to round to
    for (int i = 1; i <= numOfDec; i++)
    {
        num *= 10;
    }

    //Add/Subtract .5 to TRY to increase the number 
    //that is to the LEFT of the decimal
    if (num < 0)
    {
        num -= .5M;
    }
    else
    {
        num += .5M;
    }

    //Cut off the decimal, you have your answer!
    num = (decimal)((int)num);

    //Shift the decimal back into its proper position
    for (int i = 1; i <= numOfDec; i++)
    {
        num /= 10;
    }

    return num;

}

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

About the Author

Phylum



Canada Canada

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralAn easier way PinmemberJuMaCaBo5:01 29 Oct '04  
GeneralRe: An easier way PinmemberPhylum3:54 2 Nov '04  
GeneralConverting back to float/double... PinprotectorMarc Clifton4:36 29 Oct '04  
GeneralRe: Converting back to float/double... PinmemberJeffrey Sax5:47 31 Oct '04  
GeneralRe: Converting back to float/double... PinmemberiBos16:29 16 Jun '05  
GeneralRe: Converting back to float/double... PinmemberNegatyfus0:57 11 Jul '06  
GeneralRe: Converting back to float/double... PinmemberPhylum4:05 2 Nov '04  

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.

Permalink | Advertise | Privacy | Mobile
Web02 | 2.5.120517.1 | Last Updated 29 Oct 2004
Article Copyright 2004 by Phylum
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid