Mercurial > babel > old > mirror
annotate babel/numbers.py @ 248:587b06d55323
Updated TODO-items in number.py.
author | jonas |
---|---|
date | Sun, 12 Aug 2007 17:21:39 +0000 |
parents | f5ba90775d6c |
children | 44637ad19c35 |
rev | line source |
---|---|
3 | 1 # -*- coding: utf-8 -*- |
2 # | |
3 # Copyright (C) 2007 Edgewall Software | |
4 # All rights reserved. | |
5 # | |
247 | 6 # This software is licensed as described in the file COPYING, which |
3 | 7 # you should have received as part of this distribution. The terms |
8 # are also available at http://babel.edgewall.org/wiki/License. | |
9 # | |
10 # This software consists of voluntary contributions made by many | |
11 # individuals. For the exact contribution history, see the revision | |
12 # history and logs, available at http://babel.edgewall.org/log/. | |
13 | |
14 """Locale dependent formatting and parsing of numeric data. | |
15 | |
16 The default locale for the functions in this module is determined by the | |
17 following environment variables, in that order: | |
18 | |
19 * ``LC_NUMERIC``, | |
20 * ``LC_ALL``, and | |
21 * ``LANG`` | |
22 """ | |
248 | 23 # TODO: |
24 # Padding and rounding increments in pattern: | |
25 # - http://www.unicode.org/reports/tr35/ (Appendix G.6) | |
245
d462423feeea
Added initial support for scientific notation patterns.
jonas
parents:
235
diff
changeset
|
26 import math |
3 | 27 import re |
220 | 28 try: |
29 from decimal import Decimal | |
30 have_decimal = True | |
31 except ImportError: | |
32 have_decimal = False | |
3 | 33 |
74
d9c34d2f3d1d
More explicit module-level function names in `babel.core`. Added `Locale.negotiate` class method.
cmlenz
parents:
52
diff
changeset
|
34 from babel.core import default_locale, Locale |
3 | 35 |
36 __all__ = ['format_number', 'format_decimal', 'format_currency', | |
37 'format_percent', 'format_scientific', 'parse_number', | |
34 | 38 'parse_decimal', 'NumberFormatError'] |
3 | 39 __docformat__ = 'restructuredtext en' |
40 | |
74
d9c34d2f3d1d
More explicit module-level function names in `babel.core`. Added `Locale.negotiate` class method.
cmlenz
parents:
52
diff
changeset
|
41 LC_NUMERIC = default_locale('LC_NUMERIC') |
3 | 42 |
127 | 43 def get_currency_symbol(currency, locale=LC_NUMERIC): |
44 """Return the symbol used by the locale for the specified currency. | |
45 | |
46 >>> get_currency_symbol('USD', 'en_US') | |
47 u'$' | |
48 | |
49 :param currency: the currency code | |
50 :param locale: the `Locale` object or locale identifier | |
51 :return: the currency symbol | |
52 :rtype: `unicode` | |
53 """ | |
54 return Locale.parse(locale).currency_symbols.get(currency, currency) | |
55 | |
3 | 56 def get_decimal_symbol(locale=LC_NUMERIC): |
57 """Return the symbol used by the locale to separate decimal fractions. | |
58 | |
59 >>> get_decimal_symbol('en_US') | |
60 u'.' | |
61 | |
62 :param locale: the `Locale` object or locale identifier | |
63 :return: the decimal symbol | |
64 :rtype: `unicode` | |
65 """ | |
66 return Locale.parse(locale).number_symbols.get('decimal', u'.') | |
67 | |
246
f21b6e6a13a7
Added `Decimal` support to the scientific notation formatter and some code cleanup. This closes #52.
jonas
parents:
245
diff
changeset
|
68 def get_plus_sign_symbol(locale=LC_NUMERIC): |
f21b6e6a13a7
Added `Decimal` support to the scientific notation formatter and some code cleanup. This closes #52.
jonas
parents:
245
diff
changeset
|
69 """Return the plus sign symbol used by the current locale. |
f21b6e6a13a7
Added `Decimal` support to the scientific notation formatter and some code cleanup. This closes #52.
jonas
parents:
245
diff
changeset
|
70 |
f21b6e6a13a7
Added `Decimal` support to the scientific notation formatter and some code cleanup. This closes #52.
jonas
parents:
245
diff
changeset
|
71 >>> get_plus_sign_symbol('en_US') |
f21b6e6a13a7
Added `Decimal` support to the scientific notation formatter and some code cleanup. This closes #52.
jonas
parents:
245
diff
changeset
|
72 u'+' |
f21b6e6a13a7
Added `Decimal` support to the scientific notation formatter and some code cleanup. This closes #52.
jonas
parents:
245
diff
changeset
|
73 |
f21b6e6a13a7
Added `Decimal` support to the scientific notation formatter and some code cleanup. This closes #52.
jonas
parents:
245
diff
changeset
|
74 :param locale: the `Locale` object or locale identifier |
f21b6e6a13a7
Added `Decimal` support to the scientific notation formatter and some code cleanup. This closes #52.
jonas
parents:
245
diff
changeset
|
75 :return: the plus sign symbol |
f21b6e6a13a7
Added `Decimal` support to the scientific notation formatter and some code cleanup. This closes #52.
jonas
parents:
245
diff
changeset
|
76 :rtype: `unicode` |
f21b6e6a13a7
Added `Decimal` support to the scientific notation formatter and some code cleanup. This closes #52.
jonas
parents:
245
diff
changeset
|
77 """ |
f21b6e6a13a7
Added `Decimal` support to the scientific notation formatter and some code cleanup. This closes #52.
jonas
parents:
245
diff
changeset
|
78 return Locale.parse(locale).number_symbols.get('plusSign', u'+') |
f21b6e6a13a7
Added `Decimal` support to the scientific notation formatter and some code cleanup. This closes #52.
jonas
parents:
245
diff
changeset
|
79 |
f21b6e6a13a7
Added `Decimal` support to the scientific notation formatter and some code cleanup. This closes #52.
jonas
parents:
245
diff
changeset
|
80 def get_minus_sign_symbol(locale=LC_NUMERIC): |
f21b6e6a13a7
Added `Decimal` support to the scientific notation formatter and some code cleanup. This closes #52.
jonas
parents:
245
diff
changeset
|
81 """Return the plus sign symbol used by the current locale. |
f21b6e6a13a7
Added `Decimal` support to the scientific notation formatter and some code cleanup. This closes #52.
jonas
parents:
245
diff
changeset
|
82 |
f21b6e6a13a7
Added `Decimal` support to the scientific notation formatter and some code cleanup. This closes #52.
jonas
parents:
245
diff
changeset
|
83 >>> get_minus_sign_symbol('en_US') |
f21b6e6a13a7
Added `Decimal` support to the scientific notation formatter and some code cleanup. This closes #52.
jonas
parents:
245
diff
changeset
|
84 u'-' |
f21b6e6a13a7
Added `Decimal` support to the scientific notation formatter and some code cleanup. This closes #52.
jonas
parents:
245
diff
changeset
|
85 |
f21b6e6a13a7
Added `Decimal` support to the scientific notation formatter and some code cleanup. This closes #52.
jonas
parents:
245
diff
changeset
|
86 :param locale: the `Locale` object or locale identifier |
f21b6e6a13a7
Added `Decimal` support to the scientific notation formatter and some code cleanup. This closes #52.
jonas
parents:
245
diff
changeset
|
87 :return: the plus sign symbol |
f21b6e6a13a7
Added `Decimal` support to the scientific notation formatter and some code cleanup. This closes #52.
jonas
parents:
245
diff
changeset
|
88 :rtype: `unicode` |
f21b6e6a13a7
Added `Decimal` support to the scientific notation formatter and some code cleanup. This closes #52.
jonas
parents:
245
diff
changeset
|
89 """ |
f21b6e6a13a7
Added `Decimal` support to the scientific notation formatter and some code cleanup. This closes #52.
jonas
parents:
245
diff
changeset
|
90 return Locale.parse(locale).number_symbols.get('minusSign', u'-') |
f21b6e6a13a7
Added `Decimal` support to the scientific notation formatter and some code cleanup. This closes #52.
jonas
parents:
245
diff
changeset
|
91 |
245
d462423feeea
Added initial support for scientific notation patterns.
jonas
parents:
235
diff
changeset
|
92 def get_exponential_symbol(locale=LC_NUMERIC): |
d462423feeea
Added initial support for scientific notation patterns.
jonas
parents:
235
diff
changeset
|
93 """Return the symbol used by the locale to separate mantissa and exponent. |
d462423feeea
Added initial support for scientific notation patterns.
jonas
parents:
235
diff
changeset
|
94 |
d462423feeea
Added initial support for scientific notation patterns.
jonas
parents:
235
diff
changeset
|
95 >>> get_exponential_symbol('en_US') |
d462423feeea
Added initial support for scientific notation patterns.
jonas
parents:
235
diff
changeset
|
96 u'E' |
d462423feeea
Added initial support for scientific notation patterns.
jonas
parents:
235
diff
changeset
|
97 |
d462423feeea
Added initial support for scientific notation patterns.
jonas
parents:
235
diff
changeset
|
98 :param locale: the `Locale` object or locale identifier |
d462423feeea
Added initial support for scientific notation patterns.
jonas
parents:
235
diff
changeset
|
99 :return: the exponential symbol |
d462423feeea
Added initial support for scientific notation patterns.
jonas
parents:
235
diff
changeset
|
100 :rtype: `unicode` |
d462423feeea
Added initial support for scientific notation patterns.
jonas
parents:
235
diff
changeset
|
101 """ |
d462423feeea
Added initial support for scientific notation patterns.
jonas
parents:
235
diff
changeset
|
102 return Locale.parse(locale).number_symbols.get('exponential', u'E') |
d462423feeea
Added initial support for scientific notation patterns.
jonas
parents:
235
diff
changeset
|
103 |
3 | 104 def get_group_symbol(locale=LC_NUMERIC): |
105 """Return the symbol used by the locale to separate groups of thousands. | |
106 | |
107 >>> get_group_symbol('en_US') | |
108 u',' | |
109 | |
110 :param locale: the `Locale` object or locale identifier | |
111 :return: the group symbol | |
112 :rtype: `unicode` | |
113 """ | |
11 | 114 return Locale.parse(locale).number_symbols.get('group', u',') |
3 | 115 |
116 def format_number(number, locale=LC_NUMERIC): | |
103
1ba215a5774d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
74
diff
changeset
|
117 """Return the given number formatted for a specific locale. |
3 | 118 |
119 >>> format_number(1099, locale='en_US') | |
120 u'1,099' | |
121 | |
122 :param number: the number to format | |
123 :param locale: the `Locale` object or locale identifier | |
124 :return: the formatted number | |
125 :rtype: `unicode` | |
126 """ | |
11 | 127 # Do we really need this one? |
128 return format_decimal(number, locale=locale) | |
3 | 129 |
11 | 130 def format_decimal(number, format=None, locale=LC_NUMERIC): |
103
1ba215a5774d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
74
diff
changeset
|
131 """Return the given decimal number formatted for a specific locale. |
3 | 132 |
11 | 133 >>> format_decimal(1.2345, locale='en_US') |
134 u'1.234' | |
52 | 135 >>> format_decimal(1.2346, locale='en_US') |
136 u'1.235' | |
137 >>> format_decimal(-1.2346, locale='en_US') | |
138 u'-1.235' | |
11 | 139 >>> format_decimal(1.2345, locale='sv_SE') |
140 u'1,234' | |
52 | 141 >>> format_decimal(12345, locale='de') |
11 | 142 u'12.345' |
143 | |
3 | 144 The appropriate thousands grouping and the decimal separator are used for |
145 each locale: | |
146 | |
127 | 147 >>> format_decimal(12345.5, locale='en_US') |
148 u'12,345.5' | |
11 | 149 |
3 | 150 :param number: the number to format |
11 | 151 :param format: |
3 | 152 :param locale: the `Locale` object or locale identifier |
153 :return: the formatted decimal number | |
154 :rtype: `unicode` | |
155 """ | |
156 locale = Locale.parse(locale) | |
127 | 157 if not format: |
158 format = locale.decimal_formats.get(format) | |
159 pattern = parse_pattern(format) | |
11 | 160 return pattern.apply(number, locale) |
3 | 161 |
127 | 162 def format_currency(number, currency, format=None, locale=LC_NUMERIC): |
135 | 163 u"""Return formatted currency value. |
3 | 164 |
34 | 165 >>> format_currency(1099.98, 'USD', locale='en_US') |
127 | 166 u'$1,099.98' |
167 >>> format_currency(1099.98, 'USD', locale='es_CO') | |
235
d0cd235ede46
Upgraded to CLDR 1.5 and improved timezone formatting.
cmlenz
parents:
220
diff
changeset
|
168 u'US$ 1.099,98' |
127 | 169 >>> format_currency(1099.98, 'EUR', locale='de_DE') |
170 u'1.099,98 \\u20ac' | |
171 | |
172 The pattern can also be specified explicitly: | |
173 | |
174 >>> format_currency(1099.98, 'EUR', u'\xa4\xa4 #,##0.00', locale='en_US') | |
175 u'EUR 1,099.98' | |
3 | 176 |
28
695884591af6
* Reduce size of locale data pickles by only storing the data provided by each locale itself, and merging inherited data at runtime.
cmlenz
parents:
24
diff
changeset
|
177 :param number: the number to format |
34 | 178 :param currency: the currency code |
3 | 179 :param locale: the `Locale` object or locale identifier |
180 :return: the formatted currency value | |
181 :rtype: `unicode` | |
182 """ | |
127 | 183 locale = Locale.parse(locale) |
184 if not format: | |
185 format = locale.currency_formats.get(format) | |
186 pattern = parse_pattern(format) | |
187 return pattern.apply(number, locale, currency=currency) | |
3 | 188 |
28
695884591af6
* Reduce size of locale data pickles by only storing the data provided by each locale itself, and merging inherited data at runtime.
cmlenz
parents:
24
diff
changeset
|
189 def format_percent(number, format=None, locale=LC_NUMERIC): |
103
1ba215a5774d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
74
diff
changeset
|
190 """Return formatted percent value for a specific locale. |
24 | 191 |
192 >>> format_percent(0.34, locale='en_US') | |
193 u'34%' | |
194 >>> format_percent(25.1234, locale='en_US') | |
195 u'2,512%' | |
196 >>> format_percent(25.1234, locale='sv_SE') | |
235
d0cd235ede46
Upgraded to CLDR 1.5 and improved timezone formatting.
cmlenz
parents:
220
diff
changeset
|
197 u'2\\xa0512\\xa0%' |
24 | 198 |
128 | 199 The format pattern can also be specified explicitly: |
200 | |
201 >>> format_percent(25.1234, u'#,##0\u2030', locale='en_US') | |
202 u'25,123\u2030' | |
203 | |
24 | 204 :param number: the percent number to format |
205 :param format: | |
206 :param locale: the `Locale` object or locale identifier | |
207 :return: the formatted percent number | |
208 :rtype: `unicode` | |
209 """ | |
210 locale = Locale.parse(locale) | |
127 | 211 if not format: |
212 format = locale.percent_formats.get(format) | |
213 pattern = parse_pattern(format) | |
28
695884591af6
* Reduce size of locale data pickles by only storing the data provided by each locale itself, and merging inherited data at runtime.
cmlenz
parents:
24
diff
changeset
|
214 return pattern.apply(number, locale) |
3 | 215 |
245
d462423feeea
Added initial support for scientific notation patterns.
jonas
parents:
235
diff
changeset
|
216 def format_scientific(number, format=None, locale=LC_NUMERIC): |
d462423feeea
Added initial support for scientific notation patterns.
jonas
parents:
235
diff
changeset
|
217 """Return value formatted in scientific notation for a specific locale. |
d462423feeea
Added initial support for scientific notation patterns.
jonas
parents:
235
diff
changeset
|
218 |
d462423feeea
Added initial support for scientific notation patterns.
jonas
parents:
235
diff
changeset
|
219 >>> format_scientific(10000, locale='en_US') |
d462423feeea
Added initial support for scientific notation patterns.
jonas
parents:
235
diff
changeset
|
220 u'1E4' |
d462423feeea
Added initial support for scientific notation patterns.
jonas
parents:
235
diff
changeset
|
221 |
d462423feeea
Added initial support for scientific notation patterns.
jonas
parents:
235
diff
changeset
|
222 The format pattern can also be specified explicitly: |
d462423feeea
Added initial support for scientific notation patterns.
jonas
parents:
235
diff
changeset
|
223 |
d462423feeea
Added initial support for scientific notation patterns.
jonas
parents:
235
diff
changeset
|
224 >>> format_scientific(1234567, u'##0E00', locale='en_US') |
d462423feeea
Added initial support for scientific notation patterns.
jonas
parents:
235
diff
changeset
|
225 u'1.23E06' |
d462423feeea
Added initial support for scientific notation patterns.
jonas
parents:
235
diff
changeset
|
226 |
d462423feeea
Added initial support for scientific notation patterns.
jonas
parents:
235
diff
changeset
|
227 :param number: the number to format |
d462423feeea
Added initial support for scientific notation patterns.
jonas
parents:
235
diff
changeset
|
228 :param format: |
d462423feeea
Added initial support for scientific notation patterns.
jonas
parents:
235
diff
changeset
|
229 :param locale: the `Locale` object or locale identifier |
d462423feeea
Added initial support for scientific notation patterns.
jonas
parents:
235
diff
changeset
|
230 :return: value formatted in scientific notation. |
d462423feeea
Added initial support for scientific notation patterns.
jonas
parents:
235
diff
changeset
|
231 :rtype: `unicode` |
d462423feeea
Added initial support for scientific notation patterns.
jonas
parents:
235
diff
changeset
|
232 """ |
d462423feeea
Added initial support for scientific notation patterns.
jonas
parents:
235
diff
changeset
|
233 locale = Locale.parse(locale) |
d462423feeea
Added initial support for scientific notation patterns.
jonas
parents:
235
diff
changeset
|
234 if not format: |
d462423feeea
Added initial support for scientific notation patterns.
jonas
parents:
235
diff
changeset
|
235 format = locale.scientific_formats.get(format) |
d462423feeea
Added initial support for scientific notation patterns.
jonas
parents:
235
diff
changeset
|
236 pattern = parse_pattern(format) |
d462423feeea
Added initial support for scientific notation patterns.
jonas
parents:
235
diff
changeset
|
237 return pattern.apply(number, locale) |
3 | 238 |
34 | 239 |
240 class NumberFormatError(ValueError): | |
241 """Exception raised when a string cannot be parsed into a number.""" | |
242 | |
243 | |
3 | 244 def parse_number(string, locale=LC_NUMERIC): |
245 """Parse localized number string into a long integer. | |
246 | |
247 >>> parse_number('1,099', locale='en_US') | |
248 1099L | |
249 >>> parse_number('1.099', locale='de_DE') | |
250 1099L | |
251 | |
34 | 252 When the given string cannot be parsed, an exception is raised: |
253 | |
254 >>> parse_number('1.099,98', locale='de') | |
255 Traceback (most recent call last): | |
256 ... | |
257 NumberFormatError: '1.099,98' is not a valid number | |
258 | |
3 | 259 :param string: the string to parse |
260 :param locale: the `Locale` object or locale identifier | |
261 :return: the parsed number | |
262 :rtype: `long` | |
34 | 263 :raise `NumberFormatError`: if the string can not be converted to a number |
3 | 264 """ |
34 | 265 try: |
266 return long(string.replace(get_group_symbol(locale), '')) | |
267 except ValueError: | |
268 raise NumberFormatError('%r is not a valid number' % string) | |
3 | 269 |
270 def parse_decimal(string, locale=LC_NUMERIC): | |
271 """Parse localized decimal string into a float. | |
272 | |
273 >>> parse_decimal('1,099.98', locale='en_US') | |
274 1099.98 | |
34 | 275 >>> parse_decimal('1.099,98', locale='de') |
3 | 276 1099.98 |
277 | |
34 | 278 When the given string cannot be parsed, an exception is raised: |
279 | |
280 >>> parse_decimal('2,109,998', locale='de') | |
281 Traceback (most recent call last): | |
282 ... | |
283 NumberFormatError: '2,109,998' is not a valid decimal number | |
284 | |
3 | 285 :param string: the string to parse |
286 :param locale: the `Locale` object or locale identifier | |
287 :return: the parsed decimal number | |
288 :rtype: `float` | |
34 | 289 :raise `NumberFormatError`: if the string can not be converted to a |
290 decimal number | |
3 | 291 """ |
292 locale = Locale.parse(locale) | |
34 | 293 try: |
294 return float(string.replace(get_group_symbol(locale), '') | |
295 .replace(get_decimal_symbol(locale), '.')) | |
296 except ValueError: | |
297 raise NumberFormatError('%r is not a valid decimal number' % string) | |
11 | 298 |
299 | |
300 PREFIX_END = r'[^0-9@#.,]' | |
245
d462423feeea
Added initial support for scientific notation patterns.
jonas
parents:
235
diff
changeset
|
301 NUMBER_TOKEN = r'[0-9@#.\-,E+]' |
11 | 302 |
303 PREFIX_PATTERN = r"(?P<prefix>(?:'[^']*'|%s)*)" % PREFIX_END | |
304 NUMBER_PATTERN = r"(?P<number>%s+)" % NUMBER_TOKEN | |
305 SUFFIX_PATTERN = r"(?P<suffix>.*)" | |
306 | |
129 | 307 number_re = re.compile(r"%s%s%s" % (PREFIX_PATTERN, NUMBER_PATTERN, |
11 | 308 SUFFIX_PATTERN)) |
309 | |
220 | 310 def split_number(value): |
311 """Convert a number into a (intasstring, fractionasstring) tuple""" | |
312 if have_decimal and isinstance(value, Decimal): | |
313 text = str(value) | |
314 else: | |
315 text = ('%.9f' % value).rstrip('0') | |
316 if '.' in text: | |
317 a, b = text.split('.', 1) | |
318 if b == '0': | |
319 b = '' | |
320 else: | |
321 a, b = text, '' | |
322 return a, b | |
323 | |
214 | 324 def bankersround(value, ndigits=0): |
325 """Round a number to a given precision. | |
326 | |
327 Works like round() except that the round-half-even (banker's rounding) | |
328 algorithm is used instead of round-half-up. | |
329 | |
330 >>> bankersround(5.5, 0) | |
331 6.0 | |
332 >>> bankersround(6.5, 0) | |
333 6.0 | |
334 >>> bankersround(-6.5, 0) | |
335 -6.0 | |
220 | 336 >>> bankersround(1234.0, -2) |
214 | 337 1200.0 |
338 """ | |
339 sign = int(value < 0) and -1 or 1 | |
340 value = abs(value) | |
220 | 341 a, b = split_number(value) |
342 digits = a + b | |
214 | 343 add = 0 |
344 i = len(a) + ndigits | |
345 if i < 0 or i >= len(digits): | |
346 pass | |
347 elif digits[i] > '5': | |
348 add = 1 | |
349 elif digits[i] == '5' and digits[i-1] in '13579': | |
350 add = 1 | |
220 | 351 scale = 10**ndigits |
352 if have_decimal and isinstance(value, Decimal): | |
353 return Decimal(int(value * scale + add)) / scale * sign | |
354 else: | |
355 return float(int(value * scale + add)) / scale * sign | |
214 | 356 |
11 | 357 def parse_pattern(pattern): |
358 """Parse number format patterns""" | |
359 if isinstance(pattern, NumberPattern): | |
360 return pattern | |
361 | |
362 # Do we have a negative subpattern? | |
363 if ';' in pattern: | |
364 pattern, neg_pattern = pattern.split(';', 1) | |
365 pos_prefix, number, pos_suffix = number_re.search(pattern).groups() | |
366 neg_prefix, _, neg_suffix = number_re.search(neg_pattern).groups() | |
367 else: | |
368 pos_prefix, number, pos_suffix = number_re.search(pattern).groups() | |
369 neg_prefix = '-' + pos_prefix | |
370 neg_suffix = pos_suffix | |
245
d462423feeea
Added initial support for scientific notation patterns.
jonas
parents:
235
diff
changeset
|
371 if 'E' in number: |
d462423feeea
Added initial support for scientific notation patterns.
jonas
parents:
235
diff
changeset
|
372 number, exp = number.split('E', 1) |
d462423feeea
Added initial support for scientific notation patterns.
jonas
parents:
235
diff
changeset
|
373 else: |
d462423feeea
Added initial support for scientific notation patterns.
jonas
parents:
235
diff
changeset
|
374 exp = None |
213
b13f3bf4c208
Added support for siginificant digits in number patterns.
jonas
parents:
139
diff
changeset
|
375 if '@' in number: |
b13f3bf4c208
Added support for siginificant digits in number patterns.
jonas
parents:
139
diff
changeset
|
376 if '.' in number and '0' in number: |
b13f3bf4c208
Added support for siginificant digits in number patterns.
jonas
parents:
139
diff
changeset
|
377 raise ValueError('Significant digit patterns can not contain ' |
b13f3bf4c208
Added support for siginificant digits in number patterns.
jonas
parents:
139
diff
changeset
|
378 '"@" or "0"') |
24 | 379 if '.' in number: |
380 integer, fraction = number.rsplit('.', 1) | |
381 else: | |
382 integer = number | |
383 fraction = '' | |
11 | 384 min_frac = max_frac = 0 |
385 | |
386 def parse_precision(p): | |
387 """Calculate the min and max allowed digits""" | |
388 min = max = 0 | |
389 for c in p: | |
213
b13f3bf4c208
Added support for siginificant digits in number patterns.
jonas
parents:
139
diff
changeset
|
390 if c in '@0': |
11 | 391 min += 1 |
392 max += 1 | |
393 elif c == '#': | |
394 max += 1 | |
213
b13f3bf4c208
Added support for siginificant digits in number patterns.
jonas
parents:
139
diff
changeset
|
395 elif c == ',': |
b13f3bf4c208
Added support for siginificant digits in number patterns.
jonas
parents:
139
diff
changeset
|
396 continue |
11 | 397 else: |
398 break | |
399 return min, max | |
400 | |
401 def parse_grouping(p): | |
402 """Parse primary and secondary digit grouping | |
403 | |
404 >>> parse_grouping('##') | |
405 0, 0 | |
406 >>> parse_grouping('#,###') | |
407 3, 3 | |
408 >>> parse_grouping('#,####,###') | |
409 3, 4 | |
410 """ | |
411 width = len(p) | |
412 g1 = p.rfind(',') | |
413 if g1 == -1: | |
414 return 1000, 1000 | |
415 g1 = width - g1 - 1 | |
416 g2 = p[:-g1 - 1].rfind(',') | |
417 if g2 == -1: | |
418 return g1, g1 | |
419 g2 = width - g1 - g2 - 2 | |
420 return g1, g2 | |
421 | |
246
f21b6e6a13a7
Added `Decimal` support to the scientific notation formatter and some code cleanup. This closes #52.
jonas
parents:
245
diff
changeset
|
422 int_prec = parse_precision(integer) |
f21b6e6a13a7
Added `Decimal` support to the scientific notation formatter and some code cleanup. This closes #52.
jonas
parents:
245
diff
changeset
|
423 frac_prec = parse_precision(fraction) |
245
d462423feeea
Added initial support for scientific notation patterns.
jonas
parents:
235
diff
changeset
|
424 if exp: |
246
f21b6e6a13a7
Added `Decimal` support to the scientific notation formatter and some code cleanup. This closes #52.
jonas
parents:
245
diff
changeset
|
425 frac_prec = parse_precision(integer+fraction) |
245
d462423feeea
Added initial support for scientific notation patterns.
jonas
parents:
235
diff
changeset
|
426 exp_plus = exp.startswith('+') |
d462423feeea
Added initial support for scientific notation patterns.
jonas
parents:
235
diff
changeset
|
427 exp = exp.lstrip('+') |
246
f21b6e6a13a7
Added `Decimal` support to the scientific notation formatter and some code cleanup. This closes #52.
jonas
parents:
245
diff
changeset
|
428 exp_prec = parse_precision(exp) |
245
d462423feeea
Added initial support for scientific notation patterns.
jonas
parents:
235
diff
changeset
|
429 else: |
d462423feeea
Added initial support for scientific notation patterns.
jonas
parents:
235
diff
changeset
|
430 exp_plus = None |
246
f21b6e6a13a7
Added `Decimal` support to the scientific notation formatter and some code cleanup. This closes #52.
jonas
parents:
245
diff
changeset
|
431 exp_prec = None |
11 | 432 grouping = parse_grouping(integer) |
433 return NumberPattern(pattern, (pos_prefix, neg_prefix), | |
434 (pos_suffix, neg_suffix), grouping, | |
246
f21b6e6a13a7
Added `Decimal` support to the scientific notation formatter and some code cleanup. This closes #52.
jonas
parents:
245
diff
changeset
|
435 int_prec, frac_prec, |
f21b6e6a13a7
Added `Decimal` support to the scientific notation formatter and some code cleanup. This closes #52.
jonas
parents:
245
diff
changeset
|
436 exp_prec, exp_plus) |
11 | 437 |
438 | |
439 class NumberPattern(object): | |
24 | 440 |
11 | 441 def __init__(self, pattern, prefix, suffix, grouping, |
246
f21b6e6a13a7
Added `Decimal` support to the scientific notation formatter and some code cleanup. This closes #52.
jonas
parents:
245
diff
changeset
|
442 int_prec, frac_prec, exp_prec, exp_plus): |
11 | 443 self.pattern = pattern |
444 self.prefix = prefix | |
445 self.suffix = suffix | |
446 self.grouping = grouping | |
246
f21b6e6a13a7
Added `Decimal` support to the scientific notation formatter and some code cleanup. This closes #52.
jonas
parents:
245
diff
changeset
|
447 self.int_prec = int_prec |
f21b6e6a13a7
Added `Decimal` support to the scientific notation formatter and some code cleanup. This closes #52.
jonas
parents:
245
diff
changeset
|
448 self.frac_prec = frac_prec |
f21b6e6a13a7
Added `Decimal` support to the scientific notation formatter and some code cleanup. This closes #52.
jonas
parents:
245
diff
changeset
|
449 self.exp_prec = exp_prec |
245
d462423feeea
Added initial support for scientific notation patterns.
jonas
parents:
235
diff
changeset
|
450 self.exp_plus = exp_plus |
24 | 451 if '%' in ''.join(self.prefix + self.suffix): |
220 | 452 self.scale = 100 |
24 | 453 elif u'‰' in ''.join(self.prefix + self.suffix): |
220 | 454 self.scale = 1000 |
24 | 455 else: |
220 | 456 self.scale = 1 |
11 | 457 |
458 def __repr__(self): | |
459 return '<%s %r>' % (type(self).__name__, self.pattern) | |
460 | |
127 | 461 def apply(self, value, locale, currency=None): |
24 | 462 value *= self.scale |
246
f21b6e6a13a7
Added `Decimal` support to the scientific notation formatter and some code cleanup. This closes #52.
jonas
parents:
245
diff
changeset
|
463 is_negative = int(value < 0) |
f21b6e6a13a7
Added `Decimal` support to the scientific notation formatter and some code cleanup. This closes #52.
jonas
parents:
245
diff
changeset
|
464 if self.exp_prec: # Scientific notation |
245
d462423feeea
Added initial support for scientific notation patterns.
jonas
parents:
235
diff
changeset
|
465 value = abs(value) |
d462423feeea
Added initial support for scientific notation patterns.
jonas
parents:
235
diff
changeset
|
466 exp = int(math.floor(math.log(value, 10))) |
d462423feeea
Added initial support for scientific notation patterns.
jonas
parents:
235
diff
changeset
|
467 # Minimum number of integer digits |
246
f21b6e6a13a7
Added `Decimal` support to the scientific notation formatter and some code cleanup. This closes #52.
jonas
parents:
245
diff
changeset
|
468 if self.int_prec[0] == self.int_prec[1]: |
f21b6e6a13a7
Added `Decimal` support to the scientific notation formatter and some code cleanup. This closes #52.
jonas
parents:
245
diff
changeset
|
469 exp -= self.int_prec[0] - 1 |
245
d462423feeea
Added initial support for scientific notation patterns.
jonas
parents:
235
diff
changeset
|
470 # Exponent grouping |
246
f21b6e6a13a7
Added `Decimal` support to the scientific notation formatter and some code cleanup. This closes #52.
jonas
parents:
245
diff
changeset
|
471 elif self.int_prec[1]: |
f21b6e6a13a7
Added `Decimal` support to the scientific notation formatter and some code cleanup. This closes #52.
jonas
parents:
245
diff
changeset
|
472 exp = int(exp) / self.int_prec[1] * self.int_prec[1] |
f21b6e6a13a7
Added `Decimal` support to the scientific notation formatter and some code cleanup. This closes #52.
jonas
parents:
245
diff
changeset
|
473 if not have_decimal or not isinstance(value, Decimal): |
f21b6e6a13a7
Added `Decimal` support to the scientific notation formatter and some code cleanup. This closes #52.
jonas
parents:
245
diff
changeset
|
474 value = float(value) |
f21b6e6a13a7
Added `Decimal` support to the scientific notation formatter and some code cleanup. This closes #52.
jonas
parents:
245
diff
changeset
|
475 if exp < 0: |
f21b6e6a13a7
Added `Decimal` support to the scientific notation formatter and some code cleanup. This closes #52.
jonas
parents:
245
diff
changeset
|
476 value = value * 10**(-exp) |
f21b6e6a13a7
Added `Decimal` support to the scientific notation formatter and some code cleanup. This closes #52.
jonas
parents:
245
diff
changeset
|
477 else: |
f21b6e6a13a7
Added `Decimal` support to the scientific notation formatter and some code cleanup. This closes #52.
jonas
parents:
245
diff
changeset
|
478 value = value / 10**exp |
f21b6e6a13a7
Added `Decimal` support to the scientific notation formatter and some code cleanup. This closes #52.
jonas
parents:
245
diff
changeset
|
479 exp_sign = '' |
f21b6e6a13a7
Added `Decimal` support to the scientific notation formatter and some code cleanup. This closes #52.
jonas
parents:
245
diff
changeset
|
480 if exp < 0: |
f21b6e6a13a7
Added `Decimal` support to the scientific notation formatter and some code cleanup. This closes #52.
jonas
parents:
245
diff
changeset
|
481 exp_sign = get_minus_sign_symbol(locale) |
f21b6e6a13a7
Added `Decimal` support to the scientific notation formatter and some code cleanup. This closes #52.
jonas
parents:
245
diff
changeset
|
482 elif self.exp_plus: |
f21b6e6a13a7
Added `Decimal` support to the scientific notation formatter and some code cleanup. This closes #52.
jonas
parents:
245
diff
changeset
|
483 exp_sign = get_plus_sign_symbol(locale) |
245
d462423feeea
Added initial support for scientific notation patterns.
jonas
parents:
235
diff
changeset
|
484 exp = abs(exp) |
246
f21b6e6a13a7
Added `Decimal` support to the scientific notation formatter and some code cleanup. This closes #52.
jonas
parents:
245
diff
changeset
|
485 number = u'%s%s%s%s' % \ |
f21b6e6a13a7
Added `Decimal` support to the scientific notation formatter and some code cleanup. This closes #52.
jonas
parents:
245
diff
changeset
|
486 (self._format_sigdig(value, self.frac_prec[0], |
f21b6e6a13a7
Added `Decimal` support to the scientific notation formatter and some code cleanup. This closes #52.
jonas
parents:
245
diff
changeset
|
487 self.frac_prec[1]), |
f21b6e6a13a7
Added `Decimal` support to the scientific notation formatter and some code cleanup. This closes #52.
jonas
parents:
245
diff
changeset
|
488 get_exponential_symbol(locale), exp_sign, |
f21b6e6a13a7
Added `Decimal` support to the scientific notation formatter and some code cleanup. This closes #52.
jonas
parents:
245
diff
changeset
|
489 self._format_int(str(exp), self.exp_prec[0], |
f21b6e6a13a7
Added `Decimal` support to the scientific notation formatter and some code cleanup. This closes #52.
jonas
parents:
245
diff
changeset
|
490 self.exp_prec[1], locale)) |
245
d462423feeea
Added initial support for scientific notation patterns.
jonas
parents:
235
diff
changeset
|
491 elif '@' in self.pattern: # Is it a siginificant digits pattern? |
213
b13f3bf4c208
Added support for siginificant digits in number patterns.
jonas
parents:
139
diff
changeset
|
492 text = self._format_sigdig(abs(value), |
246
f21b6e6a13a7
Added `Decimal` support to the scientific notation formatter and some code cleanup. This closes #52.
jonas
parents:
245
diff
changeset
|
493 self.int_prec[0], |
f21b6e6a13a7
Added `Decimal` support to the scientific notation formatter and some code cleanup. This closes #52.
jonas
parents:
245
diff
changeset
|
494 self.int_prec[1]) |
213
b13f3bf4c208
Added support for siginificant digits in number patterns.
jonas
parents:
139
diff
changeset
|
495 if '.' in text: |
b13f3bf4c208
Added support for siginificant digits in number patterns.
jonas
parents:
139
diff
changeset
|
496 a, b = text.split('.') |
b13f3bf4c208
Added support for siginificant digits in number patterns.
jonas
parents:
139
diff
changeset
|
497 a = self._format_int(a, 0, 1000, locale) |
b13f3bf4c208
Added support for siginificant digits in number patterns.
jonas
parents:
139
diff
changeset
|
498 if b: |
b13f3bf4c208
Added support for siginificant digits in number patterns.
jonas
parents:
139
diff
changeset
|
499 b = get_decimal_symbol(locale) + b |
246
f21b6e6a13a7
Added `Decimal` support to the scientific notation formatter and some code cleanup. This closes #52.
jonas
parents:
245
diff
changeset
|
500 number = a + b |
213
b13f3bf4c208
Added support for siginificant digits in number patterns.
jonas
parents:
139
diff
changeset
|
501 else: |
246
f21b6e6a13a7
Added `Decimal` support to the scientific notation formatter and some code cleanup. This closes #52.
jonas
parents:
245
diff
changeset
|
502 number = self._format_int(text, 0, 1000, locale) |
213
b13f3bf4c208
Added support for siginificant digits in number patterns.
jonas
parents:
139
diff
changeset
|
503 else: # A normal number pattern |
220 | 504 a, b = split_number(bankersround(abs(value), |
246
f21b6e6a13a7
Added `Decimal` support to the scientific notation formatter and some code cleanup. This closes #52.
jonas
parents:
245
diff
changeset
|
505 self.frac_prec[1])) |
220 | 506 b = b or '0' |
246
f21b6e6a13a7
Added `Decimal` support to the scientific notation formatter and some code cleanup. This closes #52.
jonas
parents:
245
diff
changeset
|
507 a = self._format_int(a, self.int_prec[0], |
f21b6e6a13a7
Added `Decimal` support to the scientific notation formatter and some code cleanup. This closes #52.
jonas
parents:
245
diff
changeset
|
508 self.int_prec[1], locale) |
213
b13f3bf4c208
Added support for siginificant digits in number patterns.
jonas
parents:
139
diff
changeset
|
509 b = self._format_frac(b, locale) |
246
f21b6e6a13a7
Added `Decimal` support to the scientific notation formatter and some code cleanup. This closes #52.
jonas
parents:
245
diff
changeset
|
510 number = a + b |
f21b6e6a13a7
Added `Decimal` support to the scientific notation formatter and some code cleanup. This closes #52.
jonas
parents:
245
diff
changeset
|
511 retval = u'%s%s%s' % (self.prefix[is_negative], number, |
f21b6e6a13a7
Added `Decimal` support to the scientific notation formatter and some code cleanup. This closes #52.
jonas
parents:
245
diff
changeset
|
512 self.suffix[is_negative]) |
127 | 513 if u'¤' in retval: |
514 retval = retval.replace(u'¤¤', currency.upper()) | |
515 retval = retval.replace(u'¤', get_currency_symbol(currency, locale)) | |
516 return retval | |
11 | 517 |
213
b13f3bf4c208
Added support for siginificant digits in number patterns.
jonas
parents:
139
diff
changeset
|
518 def _format_sigdig(self, value, min, max): |
b13f3bf4c208
Added support for siginificant digits in number patterns.
jonas
parents:
139
diff
changeset
|
519 """Convert value to a string. |
b13f3bf4c208
Added support for siginificant digits in number patterns.
jonas
parents:
139
diff
changeset
|
520 |
b13f3bf4c208
Added support for siginificant digits in number patterns.
jonas
parents:
139
diff
changeset
|
521 The resulting string will contain between (min, max) number of |
b13f3bf4c208
Added support for siginificant digits in number patterns.
jonas
parents:
139
diff
changeset
|
522 significant digits. |
b13f3bf4c208
Added support for siginificant digits in number patterns.
jonas
parents:
139
diff
changeset
|
523 """ |
220 | 524 a, b = split_number(value) |
213
b13f3bf4c208
Added support for siginificant digits in number patterns.
jonas
parents:
139
diff
changeset
|
525 ndecimals = len(a) |
220 | 526 if a == '0' and b != '': |
213
b13f3bf4c208
Added support for siginificant digits in number patterns.
jonas
parents:
139
diff
changeset
|
527 ndecimals = 0 |
b13f3bf4c208
Added support for siginificant digits in number patterns.
jonas
parents:
139
diff
changeset
|
528 while b.startswith('0'): |
b13f3bf4c208
Added support for siginificant digits in number patterns.
jonas
parents:
139
diff
changeset
|
529 b = b[1:] |
b13f3bf4c208
Added support for siginificant digits in number patterns.
jonas
parents:
139
diff
changeset
|
530 ndecimals -= 1 |
220 | 531 a, b = split_number(bankersround(value, max - ndecimals)) |
532 digits = len((a + b).lstrip('0')) | |
533 if not digits: | |
213
b13f3bf4c208
Added support for siginificant digits in number patterns.
jonas
parents:
139
diff
changeset
|
534 digits = 1 |
b13f3bf4c208
Added support for siginificant digits in number patterns.
jonas
parents:
139
diff
changeset
|
535 # Figure out if we need to add any trailing '0':s |
b13f3bf4c208
Added support for siginificant digits in number patterns.
jonas
parents:
139
diff
changeset
|
536 if len(a) >= max and a != '0': |
b13f3bf4c208
Added support for siginificant digits in number patterns.
jonas
parents:
139
diff
changeset
|
537 return a |
b13f3bf4c208
Added support for siginificant digits in number patterns.
jonas
parents:
139
diff
changeset
|
538 if digits < min: |
b13f3bf4c208
Added support for siginificant digits in number patterns.
jonas
parents:
139
diff
changeset
|
539 b += ('0' * (min - digits)) |
b13f3bf4c208
Added support for siginificant digits in number patterns.
jonas
parents:
139
diff
changeset
|
540 if b: |
b13f3bf4c208
Added support for siginificant digits in number patterns.
jonas
parents:
139
diff
changeset
|
541 return '%s.%s' % (a, b) |
b13f3bf4c208
Added support for siginificant digits in number patterns.
jonas
parents:
139
diff
changeset
|
542 return a |
b13f3bf4c208
Added support for siginificant digits in number patterns.
jonas
parents:
139
diff
changeset
|
543 |
b13f3bf4c208
Added support for siginificant digits in number patterns.
jonas
parents:
139
diff
changeset
|
544 def _format_int(self, value, min, max, locale): |
11 | 545 width = len(value) |
546 if width < min: | |
245
d462423feeea
Added initial support for scientific notation patterns.
jonas
parents:
235
diff
changeset
|
547 value = '0' * (min - width) + value |
11 | 548 gsize = self.grouping[0] |
549 ret = '' | |
550 symbol = get_group_symbol(locale) | |
551 while len(value) > gsize: | |
552 ret = symbol + value[-gsize:] + ret | |
553 value = value[:-gsize] | |
554 gsize = self.grouping[1] | |
555 return value + ret | |
556 | |
557 def _format_frac(self, value, locale): | |
246
f21b6e6a13a7
Added `Decimal` support to the scientific notation formatter and some code cleanup. This closes #52.
jonas
parents:
245
diff
changeset
|
558 min, max = self.frac_prec |
220 | 559 if len(value) < min: |
560 value += ('0' * (min - len(value))) | |
24 | 561 if max == 0 or (min == 0 and int(value) == 0): |
11 | 562 return '' |
563 width = len(value) | |
52 | 564 while len(value) > min and value[-1] == '0': |
565 value = value[:-1] | |
11 | 566 return get_decimal_symbol(locale) + value |