From d3a7ed5bcc45000a6c3dd55d85a4caaa83299f83 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20T=C3=B6rnqvist?= Date: Thu, 16 May 2024 10:09:09 +0200 Subject: [PATCH] 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 --- django/contrib/humanize/templatetags/humanize.py | 4 +++- docs/ref/contrib/humanize.txt | 1 + tests/humanize_tests/tests.py | 6 ++++++ 3 files changed, 10 insertions(+), 1 deletion(-) diff --git a/django/contrib/humanize/templatetags/humanize.py b/django/contrib/humanize/templatetags/humanize.py index 19000c185cb..174e367a692 100644 --- a/django/contrib/humanize/templatetags/humanize.py +++ b/django/contrib/humanize/templatetags/humanize.py @@ -24,12 +24,14 @@ register = template.Library() def ordinal(value): """ Convert an integer to its ordinal as a string. 1 is '1st', 2 is '2nd', - 3 is '3rd', etc. Works for any integer. + 3 is '3rd', etc. Works for any non-negative integer. """ try: value = int(value) except (TypeError, ValueError): return value + if value < 0: + return str(value) if value % 100 in (11, 12, 13): # Translators: Ordinal format for 11 (11th), 12 (12th), and 13 (13th). value = pgettext("ordinal 11, 12, 13", "{}th").format(value) diff --git a/docs/ref/contrib/humanize.txt b/docs/ref/contrib/humanize.txt index 7c1af53ed35..1596f30b97f 100644 --- a/docs/ref/contrib/humanize.txt +++ b/docs/ref/contrib/humanize.txt @@ -143,3 +143,4 @@ Examples: * ``3`` becomes ``3rd``. You can pass in either an integer or a string representation of an integer. +Negative integers are returned unchanged. diff --git a/tests/humanize_tests/tests.py b/tests/humanize_tests/tests.py index 5e4f7f0ef7f..ab967e28747 100644 --- a/tests/humanize_tests/tests.py +++ b/tests/humanize_tests/tests.py @@ -55,6 +55,9 @@ class HumanizeTests(SimpleTestCase): "102", "103", "111", + "-0", + "-1", + "-105", "something else", None, ) @@ -70,6 +73,9 @@ class HumanizeTests(SimpleTestCase): "102nd", "103rd", "111th", + "0th", + "-1", + "-105", "something else", None, )