Opened 3 years ago

Last modified 3 years ago

#20601 new New feature

intcomma and floatformat internationalization error

Reported by: c.schmitt@… Owned by:
Component: Template system Version: 1.5
Severity: Normal Keywords: internationalization, humanize, contrib, float, floatcomma
Cc: merb 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 (10)

comment:1 Changed 3 years ago by merb

Needs documentation: unset
Needs tests: unset
Patch needs improvement: unset

comment:2 Changed 3 years ago by merb

Cc: merb added

comment:3 Changed 3 years ago by merb

Owner: changed from nobody to merb
Status: newassigned

comment:4 Changed 3 years ago by merb

Summary: intcomma and floatvalue internationalization errorintcomma and floatformat internationalization error

comment:5 in reply to:  description Changed 3 years ago by merb

Replying to c.schmitt@…:

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.

i meant floatformat not floatvalue.

comment:6 Changed 3 years ago by merb

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.

Version 3, edited 3 years ago by merb (previous) (next) (diff)

comment:7 Changed 3 years ago by merb

Type: Cleanup/optimizationBug

comment:8 Changed 3 years ago by merb

Owner: merb deleted
Status: assignednew

comment:9 Changed 3 years ago by Claude Paroz

Component: Python 2Template system
Triage Stage: UnreviewedAccepted
Type: BugNew 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 3 years ago by merb

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'?

Note: See TracTickets for help on using tickets.
Back to Top