Mercurial > babel > mirror
comparison babel/numbers.py @ 556:aa53048ad7ac trunk
remove Python 2.3 compat code for Decimal
author | fschwarz |
---|---|
date | Tue, 30 Aug 2011 20:27:34 +0000 |
parents | ca203b2af83c |
children | 4e561e6411ba |
comparison
equal
deleted
inserted
replaced
554:8f3df970f492 | 556:aa53048ad7ac |
---|---|
21 * ``LANG`` | 21 * ``LANG`` |
22 """ | 22 """ |
23 # TODO: | 23 # TODO: |
24 # Padding and rounding increments in pattern: | 24 # Padding and rounding increments in pattern: |
25 # - http://www.unicode.org/reports/tr35/ (Appendix G.6) | 25 # - http://www.unicode.org/reports/tr35/ (Appendix G.6) |
26 from decimal import Decimal | |
26 import math | 27 import math |
27 import re | 28 import re |
28 try: | |
29 from decimal import Decimal | |
30 have_decimal = True | |
31 except ImportError: | |
32 have_decimal = False | |
33 | 29 |
34 from babel.core import default_locale, Locale | 30 from babel.core import default_locale, Locale |
35 | 31 |
36 __all__ = ['format_number', 'format_decimal', 'format_currency', | 32 __all__ = ['format_number', 'format_decimal', 'format_currency', |
37 'format_percent', 'format_scientific', 'parse_number', | 33 'format_percent', 'format_scientific', 'parse_number', |
324 number_re = re.compile(r"%s%s%s" % (PREFIX_PATTERN, NUMBER_PATTERN, | 320 number_re = re.compile(r"%s%s%s" % (PREFIX_PATTERN, NUMBER_PATTERN, |
325 SUFFIX_PATTERN)) | 321 SUFFIX_PATTERN)) |
326 | 322 |
327 def split_number(value): | 323 def split_number(value): |
328 """Convert a number into a (intasstring, fractionasstring) tuple""" | 324 """Convert a number into a (intasstring, fractionasstring) tuple""" |
329 if have_decimal and isinstance(value, Decimal): | 325 if isinstance(value, Decimal): |
330 text = str(value) | 326 text = str(value) |
331 else: | 327 else: |
332 text = ('%.9f' % value).rstrip('0') | 328 text = ('%.9f' % value).rstrip('0') |
333 if '.' in text: | 329 if '.' in text: |
334 a, b = text.split('.', 1) | 330 a, b = text.split('.', 1) |
364 elif digits[i] > '5': | 360 elif digits[i] > '5': |
365 add = 1 | 361 add = 1 |
366 elif digits[i] == '5' and digits[i-1] in '13579': | 362 elif digits[i] == '5' and digits[i-1] in '13579': |
367 add = 1 | 363 add = 1 |
368 scale = 10**ndigits | 364 scale = 10**ndigits |
369 if have_decimal and isinstance(value, Decimal): | 365 if isinstance(value, Decimal): |
370 return Decimal(int(value * scale + add)) / scale * sign | 366 return Decimal(int(value * scale + add)) / scale * sign |
371 else: | 367 else: |
372 return float(int(value * scale + add)) / scale * sign | 368 return float(int(value * scale + add)) / scale * sign |
373 | 369 |
374 def parse_pattern(pattern): | 370 def parse_pattern(pattern): |
488 if self.int_prec[0] == self.int_prec[1]: | 484 if self.int_prec[0] == self.int_prec[1]: |
489 exp -= self.int_prec[0] - 1 | 485 exp -= self.int_prec[0] - 1 |
490 # Exponent grouping | 486 # Exponent grouping |
491 elif self.int_prec[1]: | 487 elif self.int_prec[1]: |
492 exp = int(exp) / self.int_prec[1] * self.int_prec[1] | 488 exp = int(exp) / self.int_prec[1] * self.int_prec[1] |
493 if not have_decimal or not isinstance(value, Decimal): | 489 if not isinstance(value, Decimal): |
494 value = float(value) | 490 value = float(value) |
495 if exp < 0: | 491 if exp < 0: |
496 value = value * 10**(-exp) | 492 value = value * 10**(-exp) |
497 else: | 493 else: |
498 value = value / 10**exp | 494 value = value / 10**exp |