|
28 | 28 | from babel._compat import decimal, string_types |
29 | 29 | from babel.localedata import locale_identifiers |
30 | 30 |
|
| 31 | +try: |
| 32 | + long |
| 33 | +except NameError: |
| 34 | + long = int |
| 35 | + |
31 | 36 |
|
32 | 37 | LC_NUMERIC = default_locale('LC_NUMERIC') |
33 | 38 |
|
@@ -332,6 +337,12 @@ def get_decimal_precision(number): |
332 | 337 | return abs(decimal_tuple.exponent) |
333 | 338 |
|
334 | 339 |
|
| 340 | +def get_decimal_quantum(precision): |
| 341 | + """Return minimal quantum of a number, as defined by precision.""" |
| 342 | + assert isinstance(precision, (int, long, decimal.Decimal)) |
| 343 | + return decimal.Decimal(10) ** (-precision) |
| 344 | + |
| 345 | + |
335 | 346 | def format_decimal( |
336 | 347 | number, format=None, locale=LC_NUMERIC, |
337 | 348 | decimal_quantization=True): |
@@ -757,15 +768,15 @@ def apply( |
757 | 768 |
|
758 | 769 | # Normalize value to only have one lead digit. |
759 | 770 | exp = value.adjusted() |
760 | | - value = value * self.quantum(exp) |
| 771 | + value = value * get_decimal_quantum(exp) |
761 | 772 | assert value.adjusted() == 0 |
762 | 773 |
|
763 | 774 | # Shift exponent and value by the minimum number of leading digits |
764 | 775 | # imposed by the rendering pattern. And always make that number |
765 | 776 | # greater or equal to 1. |
766 | 777 | lead_shift = max([1, min(self.int_prec)]) - 1 |
767 | 778 | exp = exp - lead_shift |
768 | | - value = value * self.quantum(-lead_shift) |
| 779 | + value = value * get_decimal_quantum(-lead_shift) |
769 | 780 |
|
770 | 781 | # Get exponent sign symbol. |
771 | 782 | exp_sign = '' |
@@ -879,7 +890,7 @@ def _format_int(self, value, min, max, locale): |
879 | 890 | return value + ret |
880 | 891 |
|
881 | 892 | def _quantize_value(self, value, locale, frac_prec): |
882 | | - quantum = self.quantum(frac_prec[1]) |
| 893 | + quantum = get_decimal_quantum(frac_prec[1]) |
883 | 894 | rounded = value.quantize(quantum) |
884 | 895 | a, sep, b = str(rounded).partition(".") |
885 | 896 | number = (self._format_int(a, self.int_prec[0], |
|
0 commit comments