#35852: intcomma not locale aware when given a decimal as string
-------------------------------------+-------------------------------------
Reporter: Jonathan Ströbele | Type: Bug
Status: new | Component:
| contrib.humanize
Version: 5.1 | Severity: Normal
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
The
[https://docs.djangoproject.com/en/5.1/topics/i18n/formatting/#overview
documentation] regarding localization says that for thousand separators
`USE_THOUSAND_SEPARATOR = True` or `intcomma` can be used. The
[https://docs.djangoproject.com/en/5.1/ref/contrib/humanize/#std-
templatefilter-intcomma documentation] of `intcomma` says either int/float
or string representation works.
This seems to be true for a `en` locale, but in `de` locale this doesn't
hold up if the input is a string:
{{{
en:
9999999999.11 -> 9,999,999,999.11
"9999999999.11" -> 9,999,999,999.11
de:
9999999999.11 -> 9.999.999.999,11
"9999999999.11" -> 9,999,999,999.11
}}}
In the case of a decimal value as string in the `de`-locale the output is
wrong, it should be `9.999.999.999,11` (dot (.) as separator).
This seems to be the case because `intcomma` casts the value to `int` if
its not a `float`/`Decimal` (which raises a `ValueError` for
`"9999999999.11"` and then just puts in commas as thousand separators
without any awareness of the current locale.
Also the
[https://github.com/django/django/blob/3fad712a91a8a8f6f6f904aff3d895e3b06b24c7/django/contrib/humanize/templatetags/humanize.py#L67
DocBlock] of the `intcomma` functions says "Convert an integer to a string
containing commas every three digits." wich contradicts the int/float (or
string thereof) in the documentation.
So either the function is not following the documentation or the
documentation is wrong (?).
I think this is problematic as the function is communicated as an
alternative/equivalent option to ` USE_THOUSAND_SEPARATOR=True`. When a
float value is formatted with `{{ value|floatformat:2|intcomma}}` the
`floatformat` will return a string and as such, `intcomma` is no longer
locale aware (also reported in
[https://code.djangoproject.com/ticket/33771 #33771]). This can lead to
outputs like `9,999,999,999,11` in the `de` locale:
{{{
9999999999.11 -> floatformat:2 -> "9999999999,11" -> intcomma ->
"9,999,999,999,11"
}}}
A solution could be to integrate some sort of float casting into
`intcomma`? Or remove the recommendation of using it in favor of just the
`USE_THOUSAND_SEPARATOR = True` setting and clarifying the documentation
of `intcomma`?
--
Ticket URL: <https://code.djangoproject.com/ticket/35852>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/01070192a107a54c-6d6b7f58-4109-4acc-a49f-f5f11b753bb6-000000%40eu-central-1.amazonses.com.