Opened 10 years ago

Closed 10 years ago

#22798 closed Bug (fixed)

pluralize filter should pluralize float/Decimal values between 1 and 2

Reported by: Daniel Watkins Owned by: nobody
Component: Template system Version: 1.6
Severity: Normal Keywords:
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

The pluralize filter works as expected for floats/Decimals outside of the range 1-2:

>>> pluralize(0.9)
u's'
>>> pluralize(2.1)
u's'

However, when applied to a decimal number between 1 and 2, it (incorrectly) does not pluralize:

>>> pluralize(1.2)
u''

Not only is this incorrect from an English standpoint, it also contradicts pluralize's docstring: "Returns a plural suffix if the value is not 1."

The problem appears to be at line 937 where the given value is converted directly to an integer; this chops the decimal part of the number off, making it appear to be 1.

The specific use case for this is:

{{ widget_count|floatformat:"-2" }} widget{{ widget_count|pluralize }}

This will produce "0.5 widgets", "1 widget", and "2.1 widgets" correctly, but "1.2 widget" incorrectly.

(This ticket is a descendant of #16723, but that ticket was about a broader issue that encompassed this, and so ended up being closed as invalid.)

Change History (3)

comment:1 by Aymeric Augustin, 10 years ago

Triage Stage: UnreviewedAccepted

Aaah now I get it. Yes, the current behaviour should be changed for decimals when 1 < d < 2. Sorry for closing incorrectly the other ticket.

comment:2 by mardini, 10 years ago

Has patch: set

PR: https://github.com/django/django/pull/2789
Thanks.

Edit: This bug still exists in master.

Last edited 10 years ago by mardini (previous) (diff)

comment:3 by Tim Graham <timograham@…>, 10 years ago

Resolution: fixed
Status: newclosed

In 868ff4e37c1e4cfaf7283496c24f6e711ad66005:

Fixed #22798 -- pluralize() now adds plural_suffix for any 1 < d < 2

Thanks Odd_Bloke for the report.

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