# HG changeset patch # User fschwarz # Date 1314737613 0 # Node ID 4e561e6411ba04889f5450574d363cf96e8a775c # Parent aa53048ad7acf0fec0995df1df7b2715d1f2997c Only use bankers round algorithm as a tie breaker if there are two nearest numbers, round as usual if there is only one nearest number (#267, patch by Martin) diff --git a/ChangeLog b/ChangeLog --- a/ChangeLog +++ b/ChangeLog @@ -31,6 +31,9 @@ handlers (#227, reported with initial patch by dfraser) * Fix exception if environment contains an invalid locale setting (#200) * use cPickle instead of pickle for better performance (#225) + * Only use bankers round algorithm as a tie breaker if there are two nearest + numbers, round as usual if there is only one nearest number (#267, patch by + Martin) Version 0.9.6 diff --git a/babel/numbers.py b/babel/numbers.py --- a/babel/numbers.py +++ b/babel/numbers.py @@ -361,6 +361,13 @@ add = 1 elif digits[i] == '5' and digits[i-1] in '13579': add = 1 + elif digits[i] == '5': # previous digit is even + # We round up unless all following digits are zero. + for j in xrange(i + 1, len(digits)): + if digits[j] != '0': + add = 1 + break + scale = 10**ndigits if isinstance(value, Decimal): return Decimal(int(value * scale + add)) / scale * sign diff --git a/babel/tests/numbers.py b/babel/tests/numbers.py --- a/babel/tests/numbers.py +++ b/babel/tests/numbers.py @@ -12,7 +12,6 @@ # history and logs, available at http://babel.edgewall.org/log/. from decimal import Decimal - import doctest import unittest @@ -137,10 +136,24 @@ self.assertEqual(fmt, '0E0') +class BankersRoundTestCase(unittest.TestCase): + def test_round_to_nearest_integer(self): + self.assertEqual(1, numbers.bankersround(Decimal('0.5001'))) + + def test_round_to_even_for_two_nearest_integers(self): + self.assertEqual(0, numbers.bankersround(Decimal('0.5'))) + self.assertEqual(2, numbers.bankersround(Decimal('1.5'))) + self.assertEqual(-2, numbers.bankersround(Decimal('-2.5'))) + + self.assertEqual(0, numbers.bankersround(Decimal('0.05'), ndigits=1)) + self.assertEqual(Decimal('0.2'), numbers.bankersround(Decimal('0.15'), ndigits=1)) + + def suite(): suite = unittest.TestSuite() suite.addTest(doctest.DocTestSuite(numbers)) suite.addTest(unittest.makeSuite(FormatDecimalTestCase)) + suite.addTest(unittest.makeSuite(BankersRoundTestCase)) return suite if __name__ == '__main__':