comparison 0.9.x/doc/numbers.txt @ 263:5b7d3f9f7d74 stable

Create branch for 0.9.x maintenance.
author cmlenz
date Mon, 20 Aug 2007 08:34:32 +0000
parents
children
comparison
equal deleted inserted replaced
197:79565db4faf0 263:5b7d3f9f7d74
1 .. -*- mode: rst; encoding: utf-8 -*-
2
3 ==========================
4 Number Formatting
5 ==========================
6
7
8 .. contents:: Contents
9 :depth: 2
10 .. sectnum::
11
12
13 Support for locale-specific formatting and parsing of numbers is provided by
14 the ``babel.numbers`` module:
15
16 .. code-block:: pycon
17
18 >>> from babel.numbers import format_number, format_decimal, format_percent
19
20 Examples:
21
22 .. code-block:: pycon
23
24 >>> format_decimal(1.2345, locale='en_US')
25 u'1.234'
26 >>> format_decimal(1.2345, locale='sv_SE')
27 u'1,234'
28 >>> format_decimal(12345, locale='de_DE')
29 u'12.345'
30
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 u'-1.23'
46 >>> format_decimal(-1.2345, format='#,##0.##;(#)', locale='en')
47 u'(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`: http://unicode.org/reports/tr35/#Number_Format_Patterns
53
54 +----------+-----------------------------------------------------------------+
55 | Symbol | Description |
56 +==========+=================================================================+
57 | ``0`` | Digit |
58 +----------+-----------------------------------------------------------------+
59 | ``1-9`` | '1' through '9' indicate rounding. |
60 +----------+-----------------------------------------------------------------+
61 | ``@`` | Significant digit |
62 +----------+-----------------------------------------------------------------+
63 | ``#`` | Digit, zero shows as absent |
64 +----------+-----------------------------------------------------------------+
65 | ``.`` | Decimal separator or monetary decimal separator |
66 +----------+-----------------------------------------------------------------+
67 | ``-`` | Minus sign |
68 +----------+-----------------------------------------------------------------+
69 | ``,`` | Grouping separator |
70 +----------+-----------------------------------------------------------------+
71 | ``E`` | Separates mantissa and exponent in scientific notation |
72 +----------+-----------------------------------------------------------------+
73 | ``+`` | Prefix positive exponents with localized plus sign |
74 +----------+-----------------------------------------------------------------+
75 | ``;`` | Separates positive and negative subpatterns |
76 +----------+-----------------------------------------------------------------+
77 | ``%`` | Multiply by 100 and show as percentage |
78 +----------+-----------------------------------------------------------------+
79 | ``‰`` | Multiply by 1000 and show as per mille |
80 +----------+-----------------------------------------------------------------+
81 | ``¤`` | Currency sign, replaced by currency symbol. If doubled, |
82 | | replaced by international currency symbol. If tripled, uses the |
83 | | long form of the decimal symbol. |
84 +----------+-----------------------------------------------------------------+
85 | ``'`` | Used to quote special characters in a prefix or suffix |
86 +----------+-----------------------------------------------------------------+
87 | ``*`` | Pad escape, precedes pad character |
88 +----------+-----------------------------------------------------------------+
89
90
91 Parsing Numbers
92 ===============
93
94 Babel can also parse numeric data in a locale-sensitive manner:
95
96 .. code-block:: pycon
97
98 >>> from babel.numbers import parse_decimal, parse_number
99
100 Examples:
101
102 .. code-block:: pycon
103
104 >>> parse_decimal('1,099.98', locale='en_US')
105 1099.98
106 >>> parse_decimal('1.099,98', locale='de')
107 1099.98
108 >>> parse_decimal('2,109,998', locale='de')
109 Traceback (most recent call last):
110 ...
111 NumberFormatError: '2,109,998' is not a valid decimal number
112
113 .. note:: Number parsing is not properly implemented yet
Copyright (C) 2012-2017 Edgewall Software