Opened 6 months ago

Closed 6 months ago

Last modified 5 months ago

#35443 closed Bug (fixed)

Fix negative numbers for ordinals

Reported by: Siburg Owned by: S-Tornqvist, Crustum7
Component: contrib.humanize Version: dev
Severity: Normal Keywords: humanize ordinal
Cc: Siburg, Martin Jonson Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

The ordinal template tag in /contrib/humanize/templatetags/humanize.py returns strange results for negative numbers. For example:
-1 becomes -1th
-9 becomes -9st
-8 becomes -8nd
-7 becomes -7rd

I think that can be regarded as a bug. I can think of following approaches for dealing with it.

0) The null option. Won't fix. Assume ordinals are not supposed to support negative numbers. I can live with that; I don't have a use case for them either. I only discovered this anomaly while running a unit test with a -1 input. But it does feel weird to knowingly have this issue.

1) Treat it as invalid input and just return the original input; similar to a ValueError.

2) Treat it as a positive number, and return the ordinal for its absolute value.

3) Treat it as valid input, and return the ordinal for its absolute value with a minus sign in front of it. I assume that will work for non-English languages too.

I suggest doing 3). Either way, if a decision or recommendation is made to do anything about it I can prepare a PR.

Change History (7)

comment:1 by Sarah Boyce, 6 months ago

Summary: Support negative numbers for ordinalsFix negative numbers for ordinals
Triage Stage: UnreviewedAccepted

I think option 1 with a tweak to the docs, I don't think saying -2nd makes sense to support

comment:2 by Martin Jonson, 6 months ago

Owner: changed from nobody to S-Tornqvist, Crustum7
Status: newassigned

comment:3 by Martin Jonson, 6 months ago

Cc: Martin Jonson added
Has patch: set

comment:4 by Sarah Boyce, 6 months ago

Patch needs improvement: set

comment:5 by Sarah Boyce, 6 months ago

Patch needs improvement: unset
Triage Stage: AcceptedReady for checkin
Version: 5.0dev

comment:6 by Sarah Boyce <42296566+sarahboyce@…>, 6 months ago

Resolution: fixed
Status: assignedclosed

In d3a7ed5b:

Fixed #35443 -- Changed ordinal to return negative numbers unchanged.

Previously, -1 was converted to "-1th". This has been updated to
return negative numbers "as is", so that for example -1 is
converted to "-1". This is now explicit in the docs.

Co-authored-by: Martin Jonson <artin.onson@…>

comment:7 by Sarah Boyce <42296566+sarahboyce@…>, 5 months ago

In e65b7d5:

[5.1.x] Fixed #35443 -- Changed ordinal to return negative numbers unchanged.

Previously, -1 was converted to "-1th". This has been updated to
return negative numbers "as is", so that for example -1 is
converted to "-1". This is now explicit in the docs.

Co-authored-by: Martin Jonson <artin.onson@…>

Backport of d3a7ed5bcc45000a6c3dd55d85a4caaa83299f83 from main.

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