Opened 5 years ago
Last modified 8 weeks ago
#20601 new New feature
intcomma and floatformat internationalization error
Reported by: | Owned by: | ||
---|---|---|---|
Component: | Template system | Version: | 1.10 |
Severity: | Normal | Keywords: | internationalization, humanize, contrib, float, floatcomma |
Cc: | merb, Alexey | Triage Stage: | Accepted |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
When u use floatvalue "2" and intcomma together in a template the output of intcomma won't be internationalized.
Since intcomma wont work with decimals in django 1.5.1 i tried to convert a decimal to a float in a template, but it wont give me the excepted output.
When i have the value of 1000.11 it should be 1000,11 in germany, with intcomma(float(1000,11)) i get 1.000,11. But when i use Decimal(1000,11)|floatvalue"2"|intcomma, i will get 1,000,11. Thats a bug or maybe an unwanted behavior.
Change History (13)
comment:1 Changed 5 years ago by
comment:2 Changed 5 years ago by
Cc: | merb added |
---|
comment:3 Changed 5 years ago by
Owner: | changed from nobody to merb |
---|---|
Status: | new → assigned |
comment:4 Changed 5 years ago by
Summary: | intcomma and floatvalue internationalization error → intcomma and floatformat internationalization error |
---|
comment:5 Changed 5 years ago by
comment:6 Changed 5 years ago by
This is the actual humanize function:
@register.filter(is_safe=True) def intcomma(value, use_l10n=True): """ Converts an integer to a string containing commas every three digits. For example, 3000 becomes '3,000' and 45000 becomes '45,000'. """ if settings.USE_L10N and use_l10n: try: if not isinstance(value, float): value = int(value) except (TypeError, ValueError): return intcomma(value, False) else: return number_format(value, force_grouping=True) orig = force_text(value) new = re.sub("^(-?\d+)(\d{3})", '\g<1>,\g<2>', orig) if orig == new: return new else: return intcomma(new, use_l10n)
The problem is that floatformat returns a SafeText type, so it isn't a instance of float and intcomma gets recalled with use_l10n=False. So this line gets called:
new = re.sub("^(-?\d+)(\d{3})", '\g<1>,\g<2>', orig)
This line will add an , to every 3rd place on the SafeText type so a string of:
1000,11 or 1000000,11 will be
1,000,11 or 1,000,000,11 which could get converted to an integer, so the next time intcomma gets called, it thinks that the SafeText type could now get converted to an integer and will then return himself.
So this will be an uncorrect return value it should return either the correct output of 1.000.000,11 or nothing since it isn't a valid number. But i think its better to check if isinstance(value, int) and if not it should check if isinstance(value, float) and when nothing is correct it should try to convert to float
Even this won't fix it: https://github.com/django/django/pull/785/files
Maybe its better to change floatformat to output Decimal or introduce a new Variable called SafeDecimal, that gets converted to float or Decimal when intcomma gets called.
comment:7 Changed 5 years ago by
Type: | Cleanup/optimization → Bug |
---|
comment:8 Changed 5 years ago by
Owner: | merb deleted |
---|---|
Status: | assigned → new |
comment:9 Changed 5 years ago by
Component: | Python 2 → Template system |
---|---|
Triage Stage: | Unreviewed → Accepted |
Type: | Bug → New feature |
Basically, the issue is that both intcomma
and floatformat
take numbers (or number-convertible values) to output a string representation. So chaining them is basically broken. One strategy would be to try harder to make those string representation transformed back to numbers, but then the first transformation would be lost.
I'm of the opinion that either one of these filters should be able to do all necessary transformations. For example, we could imagine that the floatformat
argument takes an optional g
suffix to indicate grouping. So the result of Decimal(1000,11)|floatvalue:"2g"
would be 1.000,11 for German (i.e. numberformat
would be passed force_grouping=True
in floatformat
filter).
comment:10 Changed 5 years ago by
wouldn't it be better to make them type safe? Like raise an error if intcomma or floatformat won't get 'numbers' or 'localized_numbers'?
comment:12 Changed 8 weeks ago by
Cc: | Alexey added |
---|
comment:13 Changed 8 weeks ago by
Should we try to parse value to float?
Something like try to find float separator with regex, and convert it before isinstance
check
Replying to c.schmitt@…:
i meant floatformat not floatvalue.