Home Forums SQL Server 7,2000 T-SQL Round to Even (aka Banker''s Rounding) - The final function RE: Round to Even (aka Banker''''s Rounding) - The final function

  • I'll try and summarize the Wiki entry that is in the code comments, so that everyone understands what it is supposed to do.

    Banker's rounding is identical to traditional rounding with a single exception. When the digit to the immediate right of the desired final display digit is a 5, and when no non-zero characters follow it, you round to the nearest even digit. That's it.

    Examples:

    When rounding to 2 decimal places...

    0.1249999999999999999999999999999999999999999 returns 0.12 using both methods

    0.1250000000000000000000000000000000000000001 returns 0.13 using both methods

    0.1250000000000000000000000000000000000000000 rounds down to .12 using Banker's rounding, since the second digit, 2, is an even number, and up to .13 using traditional rounding, since traditional rounding always rounds up at 5 or greater.

    On the other hand....

    0.1349999999999999999999999999999999999999999 returns 0.13 using both methods

    0.1350000000000000000000000000000000000000001 returns 0.14 using both methods

    0.1350000000000000000000000000000000000000000 rounds up to 0.14 using Banker's rounding, since the second digit, 3, is odd, and the nearest even number is 4 (0.14 is much closer than 0.12 to 0.135), while traditional rounding also returns 0.14, since it always rounds up at 5 or greater.

    As you can see, they behave differently very rarely, but when they do, it's intentional, so that's what we need to provide for.