Michael O'Dea-Jones' Blog

Bankers Rounding. Don’t Bank on it. It’s the Default.

They say you learn something new every day. In IT I learn at least a dozen new things every day. Did you know that there are two different rounding methods in .Net? I didn't. Now I've come across them in two different applications in the same week. They are To Even (Bankers) and Away from Zero.

To Even: When a number is halfway between two others, it is rounded toward the nearest even number.

Away From Zero: When a number is halfway between two others, it is rounded toward the nearest number that is away from zero.

The default MidpointRounding method is MidpointRounding.ToEven. The following are equivalent and both return 0D:

Double roundedValue = Math.Round(0.5D)
Double roundedValue =
Math.Round(0.5D, MidpointRounding.ToEven)

When MidpointRounding.AwayFromZero is specified then the result is 1D:

Double roundedValue = Math.Round(0.5D, MidpointRounding.AwayFromZero)

Published Sunday, November 30, 2008 9:11 AM by michael@wardyit.com

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

 

4kids said:

There are more than two rounding algorithms see http://www.diycalculator.com/sp-round.shtml (and you can assume there are more- as mathematics is creative!)

August 29, 2009 2:46 PM

Leave a Comment

(required) 
(optional)
(required) 
Submit

News

Michael is a .Net Developer who enjoys creating software and doing database work too.

Tags

No tags have been created or used yet.