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

Add decimal quantization and group separator to number formatting #244

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
52 changes: 45 additions & 7 deletions flask_babel/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -482,20 +482,33 @@ def format_number(number) -> str:
return numbers.format_decimal(number, locale=locale)


def format_decimal(number, format=None) -> str:
def format_decimal(
number, format=None, decimal_quantization=True, group_separator=True
) -> str:
"""Return the given decimal number formatted 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`.
:param group_separator: Boolean to switch group separator on/off in a
locale's number format.
:return: the formatted number
:rtype: unicode
"""
locale = get_locale()
return numbers.format_decimal(number, format=format, locale=locale)
return numbers.format_decimal(
number,
format=format,
locale=locale,
decimal_quantization=decimal_quantization,
group_separator=group_separator
)


def format_currency(
number, currency, format=None, currency_digits=True, format_type="standard"
number, currency, format=None, currency_digits=True,
format_type="standard", decimal_quantization=True, group_separator=True
) -> str:
"""Return the given number formatted for the locale in the request.

Expand All @@ -506,6 +519,10 @@ def format_currency(
[default: True]
:param format_type: the currency format type to use
[default: standard]
:param decimal_quantization: Truncate and round high-precision numbers to
the format pattern. Defaults to `True`.
:param group_separator: Boolean to switch group separator on/off in a
locale's number format.
:return: the formatted number
:rtype: unicode
"""
Expand All @@ -517,31 +534,52 @@ def format_currency(
locale=locale,
currency_digits=currency_digits,
format_type=format_type,
decimal_quantization=decimal_quantization,
group_separator=group_separator
)


def format_percent(number, format=None) -> str:
def format_percent(
number, format=None, decimal_quantization=True, group_separator=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`.
:param group_separator: Boolean to switch group separator on/off in a
locale's number format.
: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,
group_separator=group_separator
)


def format_scientific(number, format=None) -> str:
def format_scientific(number, format=None, decimal_quantization=True) -> str:
"""Return value formatted in scientific notation for the locale in 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_scientific(number, format=format, locale=locale)
return numbers.format_scientific(
number,
format=format,
locale=locale,
decimal_quantization=decimal_quantization
)


class Domain(object):
Expand Down