Mercurial > babel > old > babel-test
annotate babel/numbers.py @ 39:3b314a78015d
Move function for determining the system default locale to `babel.core`, and make it available as a class method on `Locale`.
author | cmlenz |
---|---|
date | Wed, 06 Jun 2007 11:02:24 +0000 |
parents | 48cf004aa357 |
children | fa8a27b80eb4 |
rev | line source |
---|---|
1 | 1 # -*- coding: utf-8 -*- |
2 # | |
3 # Copyright (C) 2007 Edgewall Software | |
4 # All rights reserved. | |
5 # | |
6 # This software is licensed as described in the file COPYING, which | |
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 """ | |
23 # TODO: percent and scientific formatting | |
24 | |
25 import re | |
26 | |
39
3b314a78015d
Move function for determining the system default locale to `babel.core`, and make it available as a class method on `Locale`.
cmlenz
parents:
32
diff
changeset
|
27 from babel.core import getdefault, Locale |
1 | 28 |
29 __all__ = ['format_number', 'format_decimal', 'format_currency', | |
30 'format_percent', 'format_scientific', 'parse_number', | |
32 | 31 'parse_decimal', 'NumberFormatError'] |
1 | 32 __docformat__ = 'restructuredtext en' |
33 | |
39
3b314a78015d
Move function for determining the system default locale to `babel.core`, and make it available as a class method on `Locale`.
cmlenz
parents:
32
diff
changeset
|
34 LC_NUMERIC = getdefault('LC_NUMERIC') |
1 | 35 |
36 def get_decimal_symbol(locale=LC_NUMERIC): | |
37 """Return the symbol used by the locale to separate decimal fractions. | |
38 | |
39 >>> get_decimal_symbol('en_US') | |
40 u'.' | |
41 | |
42 :param locale: the `Locale` object or locale identifier | |
43 :return: the decimal symbol | |
44 :rtype: `unicode` | |
45 """ | |
46 return Locale.parse(locale).number_symbols.get('decimal', u'.') | |
47 | |
48 def get_group_symbol(locale=LC_NUMERIC): | |
49 """Return the symbol used by the locale to separate groups of thousands. | |
50 | |
51 >>> get_group_symbol('en_US') | |
52 u',' | |
53 | |
54 :param locale: the `Locale` object or locale identifier | |
55 :return: the group symbol | |
56 :rtype: `unicode` | |
57 """ | |
9 | 58 return Locale.parse(locale).number_symbols.get('group', u',') |
1 | 59 |
60 def format_number(number, locale=LC_NUMERIC): | |
61 """Returns the given number formatted for a specific locale. | |
62 | |
63 >>> format_number(1099, locale='en_US') | |
64 u'1,099' | |
65 | |
66 :param number: the number to format | |
67 :param locale: the `Locale` object or locale identifier | |
68 :return: the formatted number | |
69 :rtype: `unicode` | |
70 """ | |
9 | 71 # Do we really need this one? |
72 return format_decimal(number, locale=locale) | |
1 | 73 |
9 | 74 def format_decimal(number, format=None, locale=LC_NUMERIC): |
1 | 75 """Returns the given decimal number formatted for a specific locale. |
76 | |
9 | 77 >>> format_decimal(1, locale='en_US') |
78 u'1' | |
79 >>> format_decimal(1.2345, locale='en_US') | |
80 u'1.234' | |
81 >>> format_decimal(1.2345, locale='sv_SE') | |
82 u'1,234' | |
83 >>> format_decimal(12345, locale='de_DE') | |
84 u'12.345' | |
85 >>> format_decimal(-1.2345, format='#,##0.##;-#', locale='sv_SE') | |
86 u'-1,23' | |
87 >>> format_decimal(-1.2345, format='#,##0.##;(#)', locale='sv_SE') | |
88 u'(1,23)' | |
89 | |
1 | 90 The appropriate thousands grouping and the decimal separator are used for |
91 each locale: | |
92 | |
9 | 93 >>> format_decimal(12345, locale='en_US') |
94 u'12,345' | |
95 | |
1 | 96 :param number: the number to format |
9 | 97 :param format: |
1 | 98 :param locale: the `Locale` object or locale identifier |
99 :return: the formatted decimal number | |
100 :rtype: `unicode` | |
101 """ | |
102 locale = Locale.parse(locale) | |
9 | 103 pattern = locale.decimal_formats.get(format) |
104 if not pattern: | |
105 pattern = parse_pattern(format) | |
106 return pattern.apply(number, locale) | |
1 | 107 |
32 | 108 def format_currency(number, currency, locale=LC_NUMERIC): |
1 | 109 """Returns formatted currency value. |
110 | |
32 | 111 >>> format_currency(1099.98, 'USD', locale='en_US') |
1 | 112 u'1,099.98' |
113 | |
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
|
114 :param number: the number to format |
32 | 115 :param currency: the currency code |
1 | 116 :param locale: the `Locale` object or locale identifier |
117 :return: the formatted currency value | |
118 :rtype: `unicode` | |
119 """ | |
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
|
120 return format_decimal(number, locale=locale) |
1 | 121 |
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
|
122 def format_percent(number, format=None, locale=LC_NUMERIC): |
22 | 123 """Returns formatted percent value for a specific locale. |
124 | |
125 >>> format_percent(0.34, locale='en_US') | |
126 u'34%' | |
127 >>> format_percent(25.1234, locale='en_US') | |
128 u'2,512%' | |
129 >>> format_percent(25.1234, locale='sv_SE') | |
130 u'2\\xa0512 %' | |
131 | |
132 :param number: the percent number to format | |
133 :param format: | |
134 :param locale: the `Locale` object or locale identifier | |
135 :return: the formatted percent number | |
136 :rtype: `unicode` | |
137 """ | |
138 locale = Locale.parse(locale) | |
139 pattern = locale.percent_formats.get(format) | |
140 if not pattern: | |
141 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
|
142 return pattern.apply(number, locale) |
1 | 143 |
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
|
144 def format_scientific(number, locale=LC_NUMERIC): |
1 | 145 raise NotImplementedError |
146 | |
32 | 147 |
148 | |
149 class NumberFormatError(ValueError): | |
150 """Exception raised when a string cannot be parsed into a number.""" | |
151 | |
152 | |
1 | 153 def parse_number(string, locale=LC_NUMERIC): |
154 """Parse localized number string into a long integer. | |
155 | |
156 >>> parse_number('1,099', locale='en_US') | |
157 1099L | |
158 >>> parse_number('1.099', locale='de_DE') | |
159 1099L | |
160 | |
32 | 161 When the given string cannot be parsed, an exception is raised: |
162 | |
163 >>> parse_number('1.099,98', locale='de') | |
164 Traceback (most recent call last): | |
165 ... | |
166 NumberFormatError: '1.099,98' is not a valid number | |
167 | |
1 | 168 :param string: the string to parse |
169 :param locale: the `Locale` object or locale identifier | |
170 :return: the parsed number | |
171 :rtype: `long` | |
32 | 172 :raise `NumberFormatError`: if the string can not be converted to a number |
1 | 173 """ |
32 | 174 try: |
175 return long(string.replace(get_group_symbol(locale), '')) | |
176 except ValueError: | |
177 raise NumberFormatError('%r is not a valid number' % string) | |
1 | 178 |
179 def parse_decimal(string, locale=LC_NUMERIC): | |
180 """Parse localized decimal string into a float. | |
181 | |
182 >>> parse_decimal('1,099.98', locale='en_US') | |
183 1099.98 | |
32 | 184 >>> parse_decimal('1.099,98', locale='de') |
1 | 185 1099.98 |
186 | |
32 | 187 When the given string cannot be parsed, an exception is raised: |
188 | |
189 >>> parse_decimal('2,109,998', locale='de') | |
190 Traceback (most recent call last): | |
191 ... | |
192 NumberFormatError: '2,109,998' is not a valid decimal number | |
193 | |
1 | 194 :param string: the string to parse |
195 :param locale: the `Locale` object or locale identifier | |
196 :return: the parsed decimal number | |
197 :rtype: `float` | |
32 | 198 :raise `NumberFormatError`: if the string can not be converted to a |
199 decimal number | |
1 | 200 """ |
201 locale = Locale.parse(locale) | |
32 | 202 try: |
203 return float(string.replace(get_group_symbol(locale), '') | |
204 .replace(get_decimal_symbol(locale), '.')) | |
205 except ValueError: | |
206 raise NumberFormatError('%r is not a valid decimal number' % string) | |
9 | 207 |
208 | |
209 PREFIX_END = r'[^0-9@#.,]' | |
210 NUMBER_TOKEN = r'[0-9@#.\-,E]' | |
211 | |
212 PREFIX_PATTERN = r"(?P<prefix>(?:'[^']*'|%s)*)" % PREFIX_END | |
213 NUMBER_PATTERN = r"(?P<number>%s+)" % NUMBER_TOKEN | |
214 SUFFIX_PATTERN = r"(?P<suffix>.*)" | |
215 | |
216 number_re = re.compile(r"%s%s%s" % (PREFIX_PATTERN, NUMBER_PATTERN, | |
217 SUFFIX_PATTERN)) | |
218 | |
219 # TODO: | |
220 # Filling | |
221 # Rounding | |
222 # Scientific notation | |
223 # Significant Digits | |
224 def parse_pattern(pattern): | |
225 """Parse number format patterns""" | |
226 if isinstance(pattern, NumberPattern): | |
227 return pattern | |
228 | |
229 # Do we have a negative subpattern? | |
230 if ';' in pattern: | |
231 pattern, neg_pattern = pattern.split(';', 1) | |
232 pos_prefix, number, pos_suffix = number_re.search(pattern).groups() | |
233 neg_prefix, _, neg_suffix = number_re.search(neg_pattern).groups() | |
234 else: | |
235 pos_prefix, number, pos_suffix = number_re.search(pattern).groups() | |
236 neg_prefix = '-' + pos_prefix | |
237 neg_suffix = pos_suffix | |
22 | 238 if '.' in number: |
239 integer, fraction = number.rsplit('.', 1) | |
240 else: | |
241 integer = number | |
242 fraction = '' | |
9 | 243 min_frac = max_frac = 0 |
244 | |
245 def parse_precision(p): | |
246 """Calculate the min and max allowed digits""" | |
247 min = max = 0 | |
248 for c in p: | |
249 if c == '0': | |
250 min += 1 | |
251 max += 1 | |
252 elif c == '#': | |
253 max += 1 | |
254 else: | |
255 break | |
256 return min, max | |
257 | |
258 def parse_grouping(p): | |
259 """Parse primary and secondary digit grouping | |
260 | |
261 >>> parse_grouping('##') | |
262 0, 0 | |
263 >>> parse_grouping('#,###') | |
264 3, 3 | |
265 >>> parse_grouping('#,####,###') | |
266 3, 4 | |
267 """ | |
268 width = len(p) | |
269 g1 = p.rfind(',') | |
270 if g1 == -1: | |
271 return 1000, 1000 | |
272 g1 = width - g1 - 1 | |
273 g2 = p[:-g1 - 1].rfind(',') | |
274 if g2 == -1: | |
275 return g1, g1 | |
276 g2 = width - g1 - g2 - 2 | |
277 return g1, g2 | |
278 | |
279 int_precision = parse_precision(integer) | |
280 frac_precision = parse_precision(fraction) | |
281 grouping = parse_grouping(integer) | |
282 int_precision = (int_precision[0], 1000) # Unlimited | |
283 return NumberPattern(pattern, (pos_prefix, neg_prefix), | |
284 (pos_suffix, neg_suffix), grouping, | |
285 int_precision, frac_precision) | |
286 | |
287 | |
288 class NumberPattern(object): | |
22 | 289 |
9 | 290 def __init__(self, pattern, prefix, suffix, grouping, |
291 int_precision, frac_precision): | |
292 self.pattern = pattern | |
293 self.prefix = prefix | |
294 self.suffix = suffix | |
295 self.grouping = grouping | |
296 self.int_precision = int_precision | |
297 self.frac_precision = frac_precision | |
22 | 298 if '%' in ''.join(self.prefix + self.suffix): |
299 self.scale = 100.0 | |
300 elif u'‰' in ''.join(self.prefix + self.suffix): | |
301 self.scale = 1000.0 | |
302 else: | |
303 self.scale = 1.0 | |
9 | 304 |
305 def __repr__(self): | |
306 return '<%s %r>' % (type(self).__name__, self.pattern) | |
307 | |
308 def apply(self, value, locale): | |
22 | 309 value *= self.scale |
9 | 310 negative = int(value < 0) |
22 | 311 a, b = str(value).split('.') |
9 | 312 a = a.lstrip('-') |
313 return '%s%s%s%s' % (self.prefix[negative], | |
314 self._format_int(a, locale), | |
315 self._format_frac(b, locale), | |
316 self.suffix[negative]) | |
317 | |
318 def _format_int(self, value, locale): | |
319 min, max = self.int_precision | |
320 width = len(value) | |
321 if width < min: | |
322 value += '0' * (min - width) | |
323 gsize = self.grouping[0] | |
324 ret = '' | |
325 symbol = get_group_symbol(locale) | |
326 while len(value) > gsize: | |
327 ret = symbol + value[-gsize:] + ret | |
328 value = value[:-gsize] | |
329 gsize = self.grouping[1] | |
330 return value + ret | |
331 | |
332 def _format_frac(self, value, locale): | |
333 min, max = self.frac_precision | |
22 | 334 if max == 0 or (min == 0 and int(value) == 0): |
9 | 335 return '' |
336 width = len(value) | |
337 if width < min: | |
338 value += '0' * (min - width) | |
339 if width > max: | |
340 value = value[:max] # FIXME: Rounding?!? | |
341 return get_decimal_symbol(locale) + value |