Skip to content

Commit

Permalink
Fixed #20601 -- Added suffix g to floatformat to force group separa…
Browse files Browse the repository at this point in the history
…tors.
  • Loading branch information
jacobtylerwalls committed Oct 12, 2020
1 parent f1f2453 commit b912e98
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 3 deletions.
16 changes: 14 additions & 2 deletions django/template/defaultfilters.py
Expand Up @@ -119,9 +119,21 @@ def floatformat(text, arg=-1):
* {{ num2|floatformat:"-3" }} displays "34"
* {{ num3|floatformat:"-3" }} displays "34.260"
Use the suffix ``g`` to force the result to be grouped
(appropriately for the locale, if settings.`USE_L10N` is True):
* {{ 6666.6666|floatformat:"2g" }} displays "6,666.67"
* {{ 10000|floatformat:"g" }} displays "10,000"
If the input float is infinity or NaN, display the string representation
of that value.
"""
force_grouping = False
if isinstance(arg, str):
force_grouping = arg.endswith('g')
arg = arg.strip('g')
if not arg:
arg = -1
try:
input_val = repr(text)
d = Decimal(input_val)
Expand All @@ -141,7 +153,7 @@ def floatformat(text, arg=-1):
return input_val

if not m and p < 0:
return mark_safe(formats.number_format('%d' % (int(d)), 0))
return mark_safe(formats.number_format('%d' % (int(d)), 0, force_grouping=force_grouping))

exp = Decimal(1).scaleb(-abs(p))
# Set the precision high enough to avoid an exception (#15789).
Expand All @@ -161,7 +173,7 @@ def floatformat(text, arg=-1):
if sign and rounded_d:
digits.append('-')
number = ''.join(reversed(digits))
return mark_safe(formats.number_format(number, abs(p)))
return mark_safe(formats.number_format(number, abs(p), force_grouping=force_grouping))


@register.filter(is_safe=True)
Expand Down
15 changes: 15 additions & 0 deletions docs/ref/templates/builtins.txt
Expand Up @@ -1732,6 +1732,17 @@ displayed. For example:
``34.26000`` ``{{ value|floatformat:"-3" }}`` ``34.260``
============ ================================ ==========

Use the suffix ``g`` to force the result to be grouped
(appropriately for the locale, if :setting:`USE_L10N` is ``True``):

============ ================================= =============
``value`` Template Output
============ ================================= =============
``34232.34`` ``{{ value|floatformat:"2g" }}`` ``34,232.34``
``34232.06`` ``{{ value|floatformat:"g" }}`` ``34,232.1``
``34232.00`` ``{{ value|floatformat:"-3g" }}`` ``34,232``
============ ================================= =============

Using ``floatformat`` with no argument is equivalent to using ``floatformat``
with an argument of ``-1``.

Expand All @@ -1740,6 +1751,10 @@ with an argument of ``-1``.
In older versions, a negative zero ``-0`` was returned for negative numbers
which round to zero.

.. versionchanged:: 3.2

The suffix ``g`` to ensure grouping by separators was added.

.. templatefilter:: force_escape

``force_escape``
Expand Down
4 changes: 3 additions & 1 deletion docs/releases/3.2.txt
Expand Up @@ -367,7 +367,9 @@ Signals
Templates
~~~~~~~~~

* ...
* Decimal place arguments to :ttag:`floatformat` may now use the suffix ``g``
to ensure the result is grouped with a separator. Further, the separator
will be appropriate for the locale if :setting:`USE_L10N` is ``True``.

Tests
~~~~~
Expand Down
5 changes: 5 additions & 0 deletions tests/i18n/tests.py
Expand Up @@ -533,6 +533,11 @@ def test_locale_independent(self):
self.assertEqual('31.12.2009 в 20:50', Template('{{ dt|date:"d.m.Y в H:i" }}').render(self.ctxt))
self.assertEqual('⌚ 10:15', Template('{{ t|time:"⌚ H:i" }}').render(self.ctxt))

def test_floatformat_force_grouping(self):
with translation.override('de'):
self.assertEqual('66.666,7', Template('{{ n|floatformat:"1g" }}').render(self.ctxt))
self.assertEqual('10.000', Template('{{ l|floatformat:"g" }}').render(self.ctxt))

@override_settings(USE_L10N=False)
def test_l10n_disabled(self):
"""
Expand Down

0 comments on commit b912e98

Please sign in to comment.