cmlenz@142: .. -*- mode: rst; encoding: utf-8 -*- cmlenz@142: cmlenz@142: ========================== cmlenz@142: Number Formatting cmlenz@142: ========================== cmlenz@142: cmlenz@142: cmlenz@142: .. contents:: Contents cmlenz@142: :depth: 2 cmlenz@142: .. sectnum:: cmlenz@142: cmlenz@142: cmlenz@142: Support for locale-specific formatting and parsing of numbers is provided by cmlenz@142: the ``babel.numbers`` module: cmlenz@142: cmlenz@142: .. code-block:: pycon cmlenz@142: cmlenz@142: >>> from babel.numbers import format_number, format_decimal, format_percent cmlenz@142: cmlenz@142: Examples: cmlenz@142: cmlenz@142: .. code-block:: pycon cmlenz@142: cmlenz@142: >>> format_decimal(1.2345, locale='en_US') cmlenz@142: u'1.234' cmlenz@142: >>> format_decimal(1.2345, locale='sv_SE') cmlenz@142: u'1,234' cmlenz@142: >>> format_decimal(12345, locale='de_DE') cmlenz@142: u'12.345' cmlenz@142: cmlenz@142: cmlenz@142: Pattern Syntax cmlenz@142: ============== cmlenz@142: cmlenz@142: While Babel makes it simple to use the appropriate number format for a given cmlenz@142: locale, you can also force it to use custom patterns. As with date/time cmlenz@142: formatting patterns, the patterns Babel supports for number formatting are cmlenz@142: based on the `Locale Data Markup Language specification`_ (LDML). cmlenz@142: cmlenz@142: Examples: cmlenz@142: cmlenz@142: .. code-block:: pycon cmlenz@142: cmlenz@142: >>> format_decimal(-1.2345, format='#,##0.##;-#', locale='en') cmlenz@142: u'-1.23' cmlenz@142: >>> format_decimal(-1.2345, format='#,##0.##;(#)', locale='en') cmlenz@142: u'(1.23)' cmlenz@142: cmlenz@142: The syntax for custom number format patterns is described in detail in the cmlenz@142: the specification. The following table is just a relatively brief overview. cmlenz@142: cmlenz@142: .. _`Locale Data Markup Language specification`: http://unicode.org/reports/tr35/#Number_Format_Patterns cmlenz@142: cmlenz@142: +----------+-----------------------------------------------------------------+ cmlenz@142: | Symbol | Description | cmlenz@142: +==========+=================================================================+ cmlenz@142: | ``0`` | Digit | cmlenz@142: +----------+-----------------------------------------------------------------+ cmlenz@142: | ``1-9`` | '1' through '9' indicate rounding. | cmlenz@142: +----------+-----------------------------------------------------------------+ cmlenz@142: | ``@`` | Significant digit | cmlenz@142: +----------+-----------------------------------------------------------------+ cmlenz@142: | ``#`` | Digit, zero shows as absent | cmlenz@142: +----------+-----------------------------------------------------------------+ cmlenz@142: | ``.`` | Decimal separator or monetary decimal separator | cmlenz@142: +----------+-----------------------------------------------------------------+ cmlenz@142: | ``-`` | Minus sign | cmlenz@142: +----------+-----------------------------------------------------------------+ cmlenz@142: | ``,`` | Grouping separator | cmlenz@142: +----------+-----------------------------------------------------------------+ cmlenz@142: | ``E`` | Separates mantissa and exponent in scientific notation | cmlenz@142: +----------+-----------------------------------------------------------------+ cmlenz@142: | ``+`` | Prefix positive exponents with localized plus sign | cmlenz@142: +----------+-----------------------------------------------------------------+ cmlenz@142: | ``;`` | Separates positive and negative subpatterns | cmlenz@142: +----------+-----------------------------------------------------------------+ cmlenz@142: | ``%`` | Multiply by 100 and show as percentage | cmlenz@142: +----------+-----------------------------------------------------------------+ cmlenz@142: | ``‰`` | Multiply by 1000 and show as per mille | cmlenz@142: +----------+-----------------------------------------------------------------+ cmlenz@142: | ``¤`` | Currency sign, replaced by currency symbol. If doubled, | cmlenz@142: | | replaced by international currency symbol. If tripled, uses the | cmlenz@142: | | long form of the decimal symbol. | cmlenz@142: +----------+-----------------------------------------------------------------+ cmlenz@142: | ``'`` | Used to quote special characters in a prefix or suffix | cmlenz@142: +----------+-----------------------------------------------------------------+ cmlenz@142: | ``*`` | Pad escape, precedes pad character | cmlenz@142: +----------+-----------------------------------------------------------------+ cmlenz@142: cmlenz@142: cmlenz@142: Parsing Numbers cmlenz@142: =============== cmlenz@142: cmlenz@142: Babel can also parse numeric data in a locale-sensitive manner: cmlenz@142: cmlenz@142: .. code-block:: pycon cmlenz@142: cmlenz@142: >>> from babel.numbers import parse_decimal, parse_number cmlenz@142: cmlenz@142: Examples: cmlenz@142: cmlenz@142: .. code-block:: pycon cmlenz@142: cmlenz@142: >>> parse_decimal('1,099.98', locale='en_US') cmlenz@142: 1099.98 cmlenz@142: >>> parse_decimal('1.099,98', locale='de') cmlenz@142: 1099.98 cmlenz@142: >>> parse_decimal('2,109,998', locale='de') cmlenz@142: Traceback (most recent call last): cmlenz@142: ... cmlenz@142: NumberFormatError: '2,109,998' is not a valid decimal number