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