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