From bc2cb443af5e9ffd3a8bb37ac46dd8b7f1253913 Mon Sep 17 00:00:00 2001 From: William Sampaio <56176702+WilliamSampaio@users.noreply.github.com> Date: Wed, 4 Oct 2023 03:47:49 -0400 Subject: [PATCH 1/3] Parameter added in format_percent When using the format_percent function, I noticed that the percentage value always rounded to an integer. That's why I set out to improve it. :) --- flask_babel/__init__.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/flask_babel/__init__.py b/flask_babel/__init__.py index 9f94d22..76a3b16 100644 --- a/flask_babel/__init__.py +++ b/flask_babel/__init__.py @@ -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: From df7fdbf9c2bcf5e1bfd3d459480905de38cfef81 Mon Sep 17 00:00:00 2001 From: William Sampaio <56176702+WilliamSampaio@users.noreply.github.com> Date: Wed, 4 Oct 2023 03:58:01 -0400 Subject: [PATCH 2/3] format_percent test --- tests/test_number_formatting.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/test_number_formatting.py b/tests/test_number_formatting.py index 2759a51..42fdfb6 100644 --- a/tests/test_number_formatting.py +++ b/tests/test_number_formatting.py @@ -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' From 3dbc99494a6db1669404f81064e20d4b9bde8369 Mon Sep 17 00:00:00 2001 From: William Sampaio <56176702+WilliamSampaio@users.noreply.github.com> Date: Wed, 4 Oct 2023 04:06:07 -0400 Subject: [PATCH 3/3] update docs --- docs/index.rst | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/docs/index.rst b/docs/index.rst index e0ea0d3..67c2c7a 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -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)