Numbers and Currencies¶
The number module provides functionality to format numbers for different locales. This includes arbitrary numbers as well as currency.
Number Formatting¶
-
babel.numbers.
format_number
(number, locale='en_US_POSIX')¶ Return the given number formatted for a specific locale.
>>> format_number(1099, locale='en_US') u'1,099' >>> format_number(1099, locale='de_DE') u'1.099'
Parameters: - number – the number to format
- locale – the Locale object or locale identifier
-
babel.numbers.
format_decimal
(number, format=None, locale='en_US_POSIX')¶ 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(1.2345, locale='de') u'1,234'
The appropriate thousands grouping and the decimal separator are used for each locale:
>>> format_decimal(12345.5, locale='en_US') u'12,345.5'
Parameters: - number – the number to format
- format –
- locale – the Locale object or locale identifier
-
babel.numbers.
format_currency
(number, currency, format=None, locale='en_US_POSIX')¶ 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'1.099,98\xa0US$' >>> format_currency(1099.98, 'EUR', locale='de_DE') u'1.099,98\xa0\u20ac'
The pattern can also be specified explicitly. The currency is placed with the ‘¤’ sign. As the sign gets repeated the format expands (¤ being the symbol, ¤¤ is the currency abbreviation and ¤¤¤ is the full name of the currency):
>>> format_currency(1099.98, 'EUR', u'¤¤ #,##0.00', locale='en_US') u'EUR 1,099.98' >>> format_currency(1099.98, 'EUR', u'#,##0.00 ¤¤¤', locale='en_US') u'1,099.98 euros'
Parameters: - number – the number to format
- currency – the currency code
- locale – the Locale object or locale identifier
-
babel.numbers.
format_percent
(number, format=None, locale='en_US_POSIX')¶ 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‰', locale='en_US') u'25,123‰'
Parameters: - number – the percent number to format
- format –
- locale – the Locale object or locale identifier
-
babel.numbers.
format_scientific
(number, format=None, locale='en_US_POSIX')¶ 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'
Parameters: - number – the number to format
- format –
- locale – the Locale object or locale identifier
Number Parsing¶
-
babel.numbers.
parse_number
(string, locale='en_US_POSIX')¶ Parse localized number string into an integer.
>>> parse_number('1,099', locale='en_US') 1099 >>> parse_number('1.099', locale='de_DE') 1099
When 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 number
Parameters: - string – the string to parse
- locale – the Locale object or locale identifier
Returns: the parsed number
Raises NumberFormatError: if the string can not be converted to a number
-
babel.numbers.
parse_decimal
(string, locale='en_US_POSIX')¶ Parse localized decimal string into a decimal.
>>> parse_decimal('1,099.98', locale='en_US') Decimal('1099.98') >>> parse_decimal('1.099,98', locale='de') Decimal('1099.98')
When 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 number
Parameters: - string – the string to parse
- locale – the Locale object or locale identifier
Raises NumberFormatError: if the string can not be converted to a decimal number
Exceptions¶
-
exception
babel.numbers.
NumberFormatError
¶ Exception raised when a string cannot be parsed into a number.
Data Access¶
-
babel.numbers.
get_currency_name
(currency, count=None, locale='en_US_POSIX')¶ Return the name used by the locale for the specified currency.
>>> get_currency_name('USD', locale='en_US') u'US Dollar'
New in version 0.9.4.
Parameters: - currency – the currency code
- count – the optional count. If provided the currency name will be pluralized to that number if possible.
- locale – the Locale object or locale identifier
-
babel.numbers.
get_currency_symbol
(currency, locale='en_US_POSIX')¶ Return the symbol used by the locale for the specified currency.
>>> get_currency_symbol('USD', locale='en_US') u'$'
Parameters: - currency – the currency code
- locale – the Locale object or locale identifier
-
babel.numbers.
get_decimal_symbol
(locale='en_US_POSIX')¶ Return the symbol used by the locale to separate decimal fractions.
>>> get_decimal_symbol('en_US') u'.'
Parameters: locale – the Locale object or locale identifier
-
babel.numbers.
get_plus_sign_symbol
(locale='en_US_POSIX')¶ Return the plus sign symbol used by the current locale.
>>> get_plus_sign_symbol('en_US') u'+'
Parameters: locale – the Locale object or locale identifier
-
babel.numbers.
get_minus_sign_symbol
(locale='en_US_POSIX')¶ Return the plus sign symbol used by the current locale.
>>> get_minus_sign_symbol('en_US') u'-'
Parameters: locale – the Locale object or locale identifier