| 1 |
.. -*- mode: rst; encoding: utf-8 -*- |
| 2 |
|
| 3 |
.. _numbers: |
| 4 |
|
| 5 |
================= |
| 6 |
Number Formatting |
| 7 |
================= |
| 8 |
|
| 9 |
|
| 10 |
Support for locale-specific formatting and parsing of numbers is provided by |
| 11 |
the ``babel.numbers`` module: |
| 12 |
|
| 13 |
.. code-block:: pycon |
| 14 |
|
| 15 |
>>> from babel.numbers import format_number, format_decimal, format_compact_decimal, format_percent |
| 16 |
|
| 17 |
Examples: |
| 18 |
|
| 19 |
.. code-block:: pycon |
| 20 |
|
| 21 |
# Numbers with decimal places |
| 22 |
>>> format_decimal(1.2345, locale='en_US') |
| 23 |
'1.234' |
| 24 |
>>> format_decimal(1.2345, locale='sv_SE') |
| 25 |
'1,234' |
| 26 |
# Integers with thousand grouping |
| 27 |
>>> format_decimal(12345, locale='de_DE') |
| 28 |
'12.345' |
| 29 |
>>> format_decimal(12345678, locale='de_DE') |
| 30 |
'12.345.678' |
| 31 |
|
| 32 |
Pattern Syntax |
| 33 |
============== |
| 34 |
|
| 35 |
While Babel makes it simple to use the appropriate number format for a given |
| 36 |
locale, you can also force it to use custom patterns. As with date/time |
| 37 |
formatting patterns, the patterns Babel supports for number formatting are |
| 38 |
based on the `Locale Data Markup Language specification`_ (LDML). |
| 39 |
|
| 40 |
Examples: |
| 41 |
|
| 42 |
.. code-block:: pycon |
| 43 |
|
| 44 |
>>> format_decimal(-1.2345, format='#,##0.##;-#', locale='en') |
| 45 |
'-1.23' |
| 46 |
>>> format_decimal(-1.2345, format='#,##0.##;(#)', locale='en') |
| 47 |
'(1.23)' |
| 48 |
|
| 49 |
The syntax for custom number format patterns is described in detail in the |
| 50 |
the specification. The following table is just a relatively brief overview. |
| 51 |
|
| 52 |
.. _`Locale Data Markup Language specification`: |
| 53 |
https://unicode.org/reports/tr35/#Number_Format_Patterns |
| 54 |
|
| 55 |
+----------+-----------------------------------------------------------------+ |
| 56 |
| Symbol | Description | |
| 57 |
+==========+=================================================================+ |
| 58 |
| ``0`` | Digit | |
| 59 |
+----------+-----------------------------------------------------------------+ |
| 60 |
| ``1-9`` | '1' through '9' indicate rounding. | |
| 61 |
+----------+-----------------------------------------------------------------+ |
| 62 |
| ``@`` | Significant digit | |
| 63 |
+----------+-----------------------------------------------------------------+ |
| 64 |
| ``#`` | Digit, zero shows as absent | |
| 65 |
+----------+-----------------------------------------------------------------+ |
| 66 |
| ``.`` | Decimal separator or monetary decimal separator | |
| 67 |
+----------+-----------------------------------------------------------------+ |
| 68 |
| ``-`` | Minus sign | |
| 69 |
+----------+-----------------------------------------------------------------+ |
| 70 |
| ``,`` | Grouping separator | |
| 71 |
+----------+-----------------------------------------------------------------+ |
| 72 |
| ``E`` | Separates mantissa and exponent in scientific notation | |
| 73 |
+----------+-----------------------------------------------------------------+ |
| 74 |
| ``+`` | Prefix positive exponents with localized plus sign | |
| 75 |
+----------+-----------------------------------------------------------------+ |
| 76 |
| ``;`` | Separates positive and negative subpatterns | |
| 77 |
+----------+-----------------------------------------------------------------+ |
| 78 |
| ``%`` | Multiply by 100 and show as percentage | |
| 79 |
+----------+-----------------------------------------------------------------+ |
| 80 |
| ``‰`` | Multiply by 1000 and show as per mille | |
| 81 |
+----------+-----------------------------------------------------------------+ |
| 82 |
| ``¤`` | Currency sign, replaced by currency symbol. If doubled, | |
| 83 |
| | replaced by international currency symbol. If tripled, uses the | |
| 84 |
| | long form of the decimal symbol. | |
| 85 |
+----------+-----------------------------------------------------------------+ |
| 86 |
| ``'`` | Used to quote special characters in a prefix or suffix | |
| 87 |
+----------+-----------------------------------------------------------------+ |
| 88 |
| ``*`` | Pad escape, precedes pad character | |
| 89 |
+----------+-----------------------------------------------------------------+ |
| 90 |
|
| 91 |
|
| 92 |
Rounding Modes |
| 93 |
============== |
| 94 |
|
| 95 |
Since Babel makes full use of Python's `Decimal`_ type to perform number |
| 96 |
rounding before formatting, users have the chance to control the rounding mode |
| 97 |
and other configurable parameters through the active `Context`_ instance. |
| 98 |
|
| 99 |
By default, Python rounding mode is ``ROUND_HALF_EVEN`` which complies with |
| 100 |
`UTS #35 section 3.3`_. Yet, the caller has the opportunity to tweak the |
| 101 |
current context before formatting a number or currency: |
| 102 |
|
| 103 |
.. code-block:: pycon |
| 104 |
|
| 105 |
>>> from babel.numbers import decimal, format_decimal |
| 106 |
>>> with decimal.localcontext(decimal.Context(rounding=decimal.ROUND_DOWN)): |
| 107 |
>>> txt = format_decimal(123.99, format='#', locale='en_US') |
| 108 |
>>> txt |
| 109 |
'123' |
| 110 |
|
| 111 |
It is also possible to use ``decimal.setcontext`` or directly modifying the |
| 112 |
instance returned by ``decimal.getcontext``. However, using a context manager |
| 113 |
is always more convenient due to the automatic restoration and the ability to |
| 114 |
nest them. |
| 115 |
|
| 116 |
Whatever mechanism is chosen, always make use of the ``decimal`` module imported |
| 117 |
from ``babel.numbers``. For efficiency reasons, Babel uses the fastest decimal |
| 118 |
implementation available, such as `cdecimal`_. These various implementation |
| 119 |
offer an identical API, but their types and instances do **not** interoperate |
| 120 |
with each other. |
| 121 |
|
| 122 |
For example, the previous example can be slightly modified to generate |
| 123 |
unexpected results on Python 2.7, with the `cdecimal`_ module installed: |
| 124 |
|
| 125 |
.. code-block:: pycon |
| 126 |
|
| 127 |
>>> from decimal import localcontext, Context, ROUND_DOWN |
| 128 |
>>> from babel.numbers import format_decimal |
| 129 |
>>> with localcontext(Context(rounding=ROUND_DOWN)): |
| 130 |
>>> txt = format_decimal(123.99, format='#', locale='en_US') |
| 131 |
>>> txt |
| 132 |
'124' |
| 133 |
|
| 134 |
Changing other parameters such as the precision may also alter the results of |
| 135 |
the number formatting functions. Remember to test your code to make sure it |
| 136 |
behaves as desired. |
| 137 |
|
| 138 |
.. _Decimal: https://docs.python.org/3/library/decimal.html#decimal-objects |
| 139 |
.. _Context: https://docs.python.org/3/library/decimal.html#context-objects |
| 140 |
.. _`UTS #35 section 3.3`: https://www.unicode.org/reports/tr35/tr35-numbers.html#Formatting |
| 141 |
.. _cdecimal: https://pypi.org/project/cdecimal/ |
| 142 |
|
| 143 |
|
| 144 |
Parsing Numbers |
| 145 |
=============== |
| 146 |
|
| 147 |
Babel can also parse numeric data in a locale-sensitive manner: |
| 148 |
|
| 149 |
.. code-block:: pycon |
| 150 |
|
| 151 |
>>> from babel.numbers import parse_decimal, parse_number |
| 152 |
|
| 153 |
Examples: |
| 154 |
|
| 155 |
.. code-block:: pycon |
| 156 |
|
| 157 |
>>> parse_decimal('1,099.98', locale='en_US') |
| 158 |
1099.98 |
| 159 |
>>> parse_decimal('1.099,98', locale='de') |
| 160 |
1099.98 |
| 161 |
>>> parse_decimal('2,109,998', locale='de') |
| 162 |
Traceback (most recent call last): |
| 163 |
... |
| 164 |
NumberFormatError: '2,109,998' is not a valid decimal number |
| 165 |
|
| 166 |
Note: as of version 2.8.0, the ``parse_number`` function has limited |
| 167 |
functionality. It can remove group symbols of certain locales from numeric |
| 168 |
strings, but may behave unexpectedly until its logic handles more encoding |
| 169 |
issues and other special cases. |
| 170 |
|
| 171 |
Examples: |
| 172 |
|
| 173 |
.. code-block:: pycon |
| 174 |
|
| 175 |
>>> parse_number('1,099', locale='en_US') |
| 176 |
1099 |
| 177 |
>>> parse_number('1.099.024', locale='de') |
| 178 |
1099024 |
| 179 |
>>> parse_number('123' + '\xa0' + '4567', locale='ru') |
| 180 |
1234567 |
| 181 |
>>> parse_number('123 4567', locale='ru') |
| 182 |
... |
| 183 |
NumberFormatError: '123 4567' is not a valid number |