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