Mercurial > babel > old > babel-test
comparison doc/formatting.txt @ 32:48cf004aa357
Started docs on number formatting/parsing.
author | cmlenz |
---|---|
date | Mon, 04 Jun 2007 12:06:25 +0000 |
parents | b6ff3e4b43e5 |
children | 3666f3d3df15 |
comparison
equal
deleted
inserted
replaced
31:b6ff3e4b43e5 | 32:48cf004aa357 |
---|---|
232 | 232 |
233 | 233 |
234 Number Formatting | 234 Number Formatting |
235 ================= | 235 ================= |
236 | 236 |
237 Support for locale-specific formatting and parsing of numbers is provided by | |
238 the ``babel.numbers`` module:: | |
239 | |
240 >>> from babel.numbers import format_number, format_decimal, format_percent | |
241 | |
242 Examples:: | |
243 | |
244 >>> format_decimal(1.2345, locale='en_US') | |
245 u'1.234' | |
246 >>> format_decimal(1.2345, locale='sv_SE') | |
247 u'1,234' | |
248 >>> format_decimal(12345, locale='de_DE') | |
249 u'12.345' | |
250 | |
237 | 251 |
238 Pattern Syntax | 252 Pattern Syntax |
239 -------------- | 253 -------------- |
240 | 254 |
255 While Babel makes it simple to use the appropriate number format for a given | |
256 locale, you can also force it to use custom patterns. As with date/time | |
257 formatting patterns, the patterns Babel supports for number formatting are | |
258 based on the `Locale Data Markup Language specification`_ (LDML). | |
259 | |
260 Examples:: | |
261 | |
262 >>> format_decimal(-1.2345, format='#,##0.##;-#', locale='en') | |
263 u'-1.23' | |
264 >>> format_decimal(-1.2345, format='#,##0.##;(#)', locale='en') | |
265 u'(1.23)' | |
266 | |
267 The syntax for custom number format patterns is described in detail in the | |
268 the specification. The following table is just a relatively brief overview. | |
269 | |
270 +----------+-----------------------------------------------------------------+ | |
271 | Symbol | Description | | |
272 +==========+=================================================================+ | |
273 | ``0`` | Digit | | |
274 +----------+-----------------------------------------------------------------+ | |
275 | ``1-9`` | '1' through '9' indicate rounding. | | |
276 +----------+-----------------------------------------------------------------+ | |
277 | ``@`` | Significant digit | | |
278 +----------+-----------------------------------------------------------------+ | |
279 | ``#`` | Digit, zero shows as absent | | |
280 +----------+-----------------------------------------------------------------+ | |
281 | ``.`` | Decimal separator or monetary decimal separator | | |
282 +----------+-----------------------------------------------------------------+ | |
283 | ``-`` | Minus sign | | |
284 +----------+-----------------------------------------------------------------+ | |
285 | ``,`` | Grouping separator | | |
286 +----------+-----------------------------------------------------------------+ | |
287 | ``E`` | Separates mantissa and exponent in scientific notation | | |
288 +----------+-----------------------------------------------------------------+ | |
289 | ``+`` | Prefix positive exponents with localized plus sign | | |
290 +----------+-----------------------------------------------------------------+ | |
291 | ``;`` | Separates positive and negative subpatterns | | |
292 +----------+-----------------------------------------------------------------+ | |
293 | ``%`` | Multiply by 100 and show as percentage | | |
294 +----------+-----------------------------------------------------------------+ | |
295 | ``‰`` | Multiply by 1000 and show as per mille | | |
296 +----------+-----------------------------------------------------------------+ | |
297 | ``¤`` | Currency sign, replaced by currency symbol. If doubled, | | |
298 | | replaced by international currency symbol. If tripled, uses the | | |
299 | | long form of the decimal symbol. | | |
300 +----------+-----------------------------------------------------------------+ | |
301 | ``'`` | Used to quote special characters in a prefix or suffix | | |
302 +----------+-----------------------------------------------------------------+ | |
303 | ``*`` | Pad escape, precedes pad character | | |
304 +----------+-----------------------------------------------------------------+ | |
305 | |
241 | 306 |
242 Parsing Numbers | 307 Parsing Numbers |
243 --------------- | 308 --------------- |
309 | |
310 Babel can also parse numeric data in a locale-sensitive manner:: | |
311 | |
312 >>> from babel.dates import parse_decimal, parse_number | |
313 | |
314 Examples:: | |
315 | |
316 >>> parse_decimal('1,099.98', locale='en_US') | |
317 1099.98 | |
318 >>> parse_decimal('1.099,98', locale='de') | |
319 1099.98 | |
320 >>> parse_decimal('2,109,998', locale='de') | |
321 Traceback (most recent call last): | |
322 ... | |
323 NumberFormatError: '2,109,998' is not a valid decimal number |