Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve format_percent #235

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -186,8 +186,10 @@ u'1.235'
'$1,099.98'

>>> from flask_babel import format_percent
>>> format_percent(0.34)
>>> format_percent(0.3399)
'34%'
>>> format_percent(0.3399, decimal_quantization=False)
'33.99%'

>>> from flask_babel import format_scientific
>>> format_scientific(10000)
Expand Down
6 changes: 4 additions & 2 deletions flask_babel/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -520,16 +520,18 @@ def format_currency(number, currency, format=None, currency_digits=True,
)


def format_percent(number, format=None) -> str:
def format_percent(number, format=None, decimal_quantization=True) -> str:
"""Return formatted percent value for the locale in the request.

:param number: the number to format
:param format: the format to use
:param decimal_quantization: Truncate and round high-precision numbers to
the format pattern. Defaults to `True`.
:return: the formatted percent number
:rtype: unicode
"""
locale = get_locale()
return numbers.format_percent(number, format=format, locale=locale)
return numbers.format_percent(number, format=format, locale=locale, decimal_quantization=decimal_quantization)


def format_scientific(number, format=None) -> str:
Expand Down
2 changes: 2 additions & 0 deletions tests/test_number_formatting.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,6 @@ def test_basics():
assert babel.format_decimal(Decimal('1010.99')) == u'1,010.99'
assert babel.format_currency(n, 'USD') == '$1,099.00'
assert babel.format_percent(0.19) == '19%'
assert babel.format_percent(0.1999,
decimal_quantization=False) == '19.99%'
assert babel.format_scientific(10000) == u'1E4'