annotate babel/numbers.py @ 522:0c202a7562ee

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