To round a penny

 Posted by  Programming  Add comments  Tagged with:
Jan 152013
 

Canada decided to withdraw penny coin. In order to keep everything going smoothly and fair they also specified rules for rounding. Rules that are not readily available in any programming language.

In time before rounding functions you would round stuff by multiplying by 100 (in case of pennies) and adding 0.5. You would truncate resulting number and divide it with 100 thus abusing implicit rounding when stuff gets converted to int. Something like this:

int pennies = (int)(value * 100 + 0.5);
return pennies / 100.0;

With nickel rounding we shall use almost same algorithm:

private static Decimal RoundPenny(Decimal value) {
    var pennies = Math.Round(value * 100, 0);
    var nickels = Math.Truncate((pennies + 2) / 5);
    var roundedPennies = nickels * 5;
    return roundedPennies / 100;
}

First we just get a number of pennies. Then we increase number by two pennies (half of nickel) and divide everything by one nickel. Truncating result gives us number of nickel coins that we need for a given penny count. Multiplication by five converts everything back to pennies and final division by 100 ensures that dollars are returned.

Code is here.

PS: Yes, you can write this more concise but I think that this makes example much more readable. Concise example would be:

return Math.Round(value * 20, 0) / 20;

  One Response to “To round a penny”

Comments (1)
  1. I’m still learning from you, while I’m making my way to the top as well. I certainly love reading all that is written on your website.Keep the information coming. I liked it!

 Leave a Reply

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>