#16723 closed Bug (invalid)
Pluralize filter shouldn't pluralize decimal/float values between 1 and 2
| Reported by: | Ryan Fugger | Owned by: | |
|---|---|---|---|
| Component: | Template system | Version: | dev |
| Severity: | Normal | Keywords: | |
| Cc: | arv@…, adam@…, Daniel Watkins | 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's support for floating point/decimal numbers is buggy. Its check for plurality is:
if int(value) != 1
which unfortunately catches floats 1.0 < x < 2.0 that shouldn't be pluralized in English.
Example:
pound{{ weight|pluralize:"s" }}
When weight is 1.3, this gives "1.3 pound", which is incorrect. "1.3 pounds" is the expected result.
The filter should probably also pluralize 1.0 when it is given as a floating point/decimal number, because native speakers will generally pluralize all decimal numbers. ("One-point-oh hours," not "One-point-oh hour".)
Reference:
http://www.englishforums.com/English/1119PluralOrSingular/wdvmb/post.htm#684376
Attachments (1)
Change History (10)
by , 14 years ago
| Attachment: | django-2011-08-29-pluralize-16723.patch added |
|---|
comment:1 by , 14 years ago
| Cc: | added |
|---|---|
| Has patch: | set |
comment:2 by , 14 years ago
| Triage Stage: | Unreviewed → Accepted |
|---|
The docs say that pluralize "returns a plural suffix if the value is not 1".
It looks like int(value) is only intended to convert a string to an int and pluralize wasn't designed to deal with float values.
An alternative fix could be to change the check to if str(value) != "1". Thus 1.0 will be pluralized and it's still a one-liner.
comment:3 by , 13 years ago
| Owner: | changed from to |
|---|---|
| Status: | new → assigned |
comment:4 by , 13 years ago
| Resolution: | → invalid |
|---|---|
| Status: | assigned → closed |
Re-reading the original report and the first comment closely, they actually say that all floats should trigger pluralization, even 1.0.
This makes the pluralize filter unnecessary when used with floats.
comment:5 by , 12 years ago
| Cc: | added |
|---|---|
| Resolution: | invalid |
| Status: | closed → new |
This does cause a problem with DecimalField. If I do:
MyModel.objects.create(decimal_field=Decimal('1.0'))
then this comes out of the ORM as:
Decimal('1')
which the templating system will display as "1" (rather than "1.0" or another float-y representation). (This is also the case if I pass in the float 1.0.)
Therefore, I can't "hard-code" the pluralised noun, because I might end up with "1 widgets" rather than "1 widget". If I use |pluralize, then I end up with "1 widget" but also "1.2 widget".
I'm happy to be corrected, but it seems to me that this is a valid bug.
comment:6 by , 12 years ago
| Status: | new → assigned |
|---|
comment:7 by , 12 years ago
| Owner: | removed |
|---|---|
| Status: | assigned → new |
comment:8 by , 11 years ago
| Resolution: | → invalid |
|---|---|
| Status: | new → closed |
I don't understand the last comment, Decimal('1.0') is handled by the |pluralize filter as a singular value and is displayed as 1 in the template. So everything is correct. I'm going to add a test.
Patch to fix w/ tests attached.
I agree that it would make sense for 1.0 to also be pluralized, however, I didnt make that change in this patch for three reasons:
{{ weight|floatformat:1 }} poundsWith this patch, for "1 pound" & "1.1 pounds", we can do:
{{ weight|floatformat }} pound{{ weight|pluralize }}This is consistent with the decision made in #12380.