Did I find the right examples for you? yes no      Crawl my project      Python Jobs

# babel.numbers.format_decimal

All Samples(117)  |  Call(86)  |  Derive(0)  |  Import(31)

```        def format_decimal(number, format=None, locale=LC_NUMERIC):
u"""Return the given decimal number formatted for a specific locale.

>>> format_decimal(1.2345, locale='en_US')
u'1.234'
>>> format_decimal(1.2346, locale='en_US')
u'1.235'
>>> format_decimal(-1.2346, locale='en_US')
u'-1.235'
>>> format_decimal(1.2345, locale='sv_SE')
u'1,234'
>>> format_decimal(1.2345, locale='de')
u'1,234'

The appropriate thousands grouping and the decimal separator are used for
each locale:

>>> format_decimal(12345.5, locale='en_US')
u'12,345.5'

:param number: the number to format
:param format:
:param locale: the `Locale` object or locale identifier
"""
locale = Locale.parse(locale)
if not format:
format = locale.decimal_formats.get(format)
pattern = parse_pattern(format)
return pattern.apply(number, locale)
```

```from babel.dates import (format_date as _format_date,
format_datetime as _format_datetime, format_time as _format_time)
from babel.numbers import format_decimal as _format_decimal
from babel.support import Translations
from babel.util import LOCALTZ
```
```    :rtype: `unicode`
"""
return _format_decimal(number, locale=translator.locale)

def format_time(time=None, format='medium', tzinfo=None):
```

```def format_number(number):
return numbers.format_decimal(number, locale=get_locale())

def list_languages():
```

```from flask import Flask, render_template, request
from babel.numbers import format_decimal

```
```def number_format(number):
"""Custom Jinja filter for adding a better number display, according
to the locale
"""
return format_decimal(number, locale='en_US')
```

```def format_number(number):
return numbers.format_decimal(number, locale=get_locale())

def list_languages():
```

```def format_decimal(*args, **kw):
return babel.numbers.format_decimal(*args, locale=get_locale(), **kw)

```

```    def test_formatting_of_very_small_decimals(self):
# previously formatting very small decimals could lead to a type error
# because the Decimal->string conversion was too simple (see #214)
number = Decimal("7E-7")
fmt = numbers.format_decimal(number, format="@@@", locale='en_US')
```
```def test_format_decimal():
assert numbers.format_decimal(1.2345, locale='en_US') == u'1.234'
assert numbers.format_decimal(1.2346, locale='en_US') == u'1.235'
assert numbers.format_decimal(-1.2346, locale='en_US') == u'-1.235'
assert numbers.format_decimal(1.2345, locale='sv_SE') == u'1,234'
```

```    def test_formatting_of_very_small_decimals(self):
# previously formatting very small decimals could lead to a type error
# because the Decimal->string conversion was too simple (see #214)
number = Decimal("7E-7")
fmt = numbers.format_decimal(number, format="@@@", locale='en_US')
```
```def test_format_decimal():
assert numbers.format_decimal(1.2345, locale='en_US') == u'1.234'
assert numbers.format_decimal(1.2346, locale='en_US') == u'1.235'
assert numbers.format_decimal(-1.2346, locale='en_US') == u'-1.235'
assert numbers.format_decimal(1.2345, locale='sv_SE') == u'1,234'
```

```    def test_formatting_of_very_small_decimals(self):
# previously formatting very small decimals could lead to a type error
# because the Decimal->string conversion was too simple (see #214)
number = Decimal("7E-7")
fmt = numbers.format_decimal(number, format="@@@", locale='en_US')
```
```def test_format_decimal():
assert numbers.format_decimal(1.2345, locale='en_US') == u'1.234'
assert numbers.format_decimal(1.2346, locale='en_US') == u'1.235'
assert numbers.format_decimal(-1.2346, locale='en_US') == u'-1.235'
assert numbers.format_decimal(1.2345, locale='sv_SE') == u'1,234'
```

```from babel import Locale