babel.numbers
Locale dependent formatting and parsing of numeric data.
The default locale for the functions in this module is determined by the following environment variables, in that order:
- LC_NUMERIC,
- LC_ALL, and
- LANG
get_currency_name(currency, locale=LC_NUMERIC)Return the name used by the locale for the specified currency.
>>> get_currency_name('USD', 'en_US') u'US Dollar'param currency: the currency code param locale: the Locale object or locale identifier return: the currency symbol rtype: unicode since: version 0.9.4 get_currency_symbol(currency, locale=LC_NUMERIC)Return the symbol used by the locale for the specified currency.
>>> get_currency_symbol('USD', 'en_US') u'$'param currency: the currency code param locale: the Locale object or locale identifier return: the currency symbol rtype: unicode get_decimal_symbol(locale=LC_NUMERIC)Return the symbol used by the locale to separate decimal fractions.
>>> get_decimal_symbol('en_US') u'.'param locale: the Locale object or locale identifier return: the decimal symbol rtype: unicode get_plus_sign_symbol(locale=LC_NUMERIC)Return the plus sign symbol used by the current locale.
>>> get_plus_sign_symbol('en_US') u'+'param locale: the Locale object or locale identifier return: the plus sign symbol rtype: unicode get_minus_sign_symbol(locale=LC_NUMERIC)Return the plus sign symbol used by the current locale.
>>> get_minus_sign_symbol('en_US') u'-'param locale: the Locale object or locale identifier return: the plus sign symbol rtype: unicode get_exponential_symbol(locale=LC_NUMERIC)Return the symbol used by the locale to separate mantissa and exponent.
>>> get_exponential_symbol('en_US') u'E'param locale: the Locale object or locale identifier return: the exponential symbol rtype: unicode get_group_symbol(locale=LC_NUMERIC)Return the symbol used by the locale to separate groups of thousands.
>>> get_group_symbol('en_US') u','param locale: the Locale object or locale identifier return: the group symbol rtype: unicode format_number(number, locale=LC_NUMERIC)Return the given number formatted for a specific locale.
>>> format_number(1099, locale='en_US') u'1,099'
param number: the number to format param locale: the Locale object or locale identifier return: the formatted number rtype: unicode format_decimal(number, format=None, locale=LC_NUMERIC)Return the given decimal number formatted for a specific locale.
>>> format_decimal(1.2345, locale='en_US') u'1.234' >>> format_decimal(1.2346, locale='en_US') u'1.235' >>> format_decimal(-1.2346, locale='en_US') u'-1.235' >>> format_decimal(1.2345, locale='sv_SE') u'1,234' >>> format_decimal(12345, locale='de') u'12.345'
The appropriate thousands grouping and the decimal separator are used for each locale:
>>> format_decimal(12345.5, locale='en_US') u'12,345.5'
param number: the number to format param format: param locale: the Locale object or locale identifier return: the formatted decimal number rtype: unicode format_currency(number, currency, format=None, locale=LC_NUMERIC)Return formatted currency value.
>>> format_currency(1099.98, 'USD', locale='en_US') u'$1,099.98' >>> format_currency(1099.98, 'USD', locale='es_CO') u'US$\xa01.099,98' >>> format_currency(1099.98, 'EUR', locale='de_DE') u'1.099,98\xa0\u20ac'
The pattern can also be specified explicitly:
>>> format_currency(1099.98, 'EUR', u'¤¤ #,##0.00', locale='en_US') u'EUR 1,099.98'
param number: the number to format param currency: the currency code param locale: the Locale object or locale identifier return: the formatted currency value rtype: unicode format_percent(number, format=None, locale=LC_NUMERIC)Return formatted percent value for a specific locale.
>>> format_percent(0.34, locale='en_US') u'34%' >>> format_percent(25.1234, locale='en_US') u'2,512%' >>> format_percent(25.1234, locale='sv_SE') u'2\xa0512\xa0%'
The format pattern can also be specified explicitly:
>>> format_percent(25.1234, u'#,##0\u2030', locale='en_US') u'25,123\u2030'
param number: the percent number to format param format: param locale: the Locale object or locale identifier return: the formatted percent number rtype: unicode format_scientific(number, format=None, locale=LC_NUMERIC)Return value formatted in scientific notation for a specific locale.
>>> format_scientific(10000, locale='en_US') u'1E4'
The format pattern can also be specified explicitly:
>>> format_scientific(1234567, u'##0E00', locale='en_US') u'1.23E06'
param number: the number to format param format: param locale: the Locale object or locale identifier return: value formatted in scientific notation. rtype: unicode NumberFormatError
Exception raised when a string cannot be parsed into a number.
parse_number(string, locale=LC_NUMERIC)Parse localized number string into a long integer.
>>> parse_number('1,099', locale='en_US') 1099L >>> parse_number('1.099', locale='de_DE') 1099LWhen the given string cannot be parsed, an exception is raised:
>>> parse_number('1.099,98', locale='de') Traceback (most recent call last): ... NumberFormatError: '1.099,98' is not a valid numberparam string: the string to parse param locale: the Locale object or locale identifier return: the parsed number rtype: long raise NumberFormatError: if the string can not be converted to a number parse_decimal(string, locale=LC_NUMERIC)Parse localized decimal string into a float.
>>> parse_decimal('1,099.98', locale='en_US') 1099.98 >>> parse_decimal('1.099,98', locale='de') 1099.98When the given string cannot be parsed, an exception is raised:
>>> parse_decimal('2,109,998', locale='de') Traceback (most recent call last): ... NumberFormatError: '2,109,998' is not a valid decimal numberparam string: the string to parse param locale: the Locale object or locale identifier return: the parsed decimal number rtype: float raise NumberFormatError: if the string can not be converted to a decimal number split_number(value)Convert a number into a (intasstring, fractionasstring) tuple
bankersround(value, ndigits=0)Round a number to a given precision.
Works like round() except that the round-half-even (banker's rounding) algorithm is used instead of round-half-up.
>>> bankersround(5.5, 0) 6.0 >>> bankersround(6.5, 0) 6.0 >>> bankersround(-6.5, 0) -6.0 >>> bankersround(1234.0, -2) 1200.0
parse_pattern(pattern)Parse number format patterns
NumberPattern
(Not documented)
apply(self, value, locale, currency=None)(Not documented)
See ApiDocs, Documentation
