mirror of
https://github.com/django/django.git
synced 2024-12-22 09:05:43 +00:00
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@gmail.com>
This commit is contained in:
parent
b049bec7cf
commit
d3a7ed5bcc
@ -24,12 +24,14 @@ register = template.Library()
|
|||||||
def ordinal(value):
|
def ordinal(value):
|
||||||
"""
|
"""
|
||||||
Convert an integer to its ordinal as a string. 1 is '1st', 2 is '2nd',
|
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:
|
try:
|
||||||
value = int(value)
|
value = int(value)
|
||||||
except (TypeError, ValueError):
|
except (TypeError, ValueError):
|
||||||
return value
|
return value
|
||||||
|
if value < 0:
|
||||||
|
return str(value)
|
||||||
if value % 100 in (11, 12, 13):
|
if value % 100 in (11, 12, 13):
|
||||||
# Translators: Ordinal format for 11 (11th), 12 (12th), and 13 (13th).
|
# Translators: Ordinal format for 11 (11th), 12 (12th), and 13 (13th).
|
||||||
value = pgettext("ordinal 11, 12, 13", "{}th").format(value)
|
value = pgettext("ordinal 11, 12, 13", "{}th").format(value)
|
||||||
|
@ -143,3 +143,4 @@ Examples:
|
|||||||
* ``3`` becomes ``3rd``.
|
* ``3`` becomes ``3rd``.
|
||||||
|
|
||||||
You can pass in either an integer or a string representation of an integer.
|
You can pass in either an integer or a string representation of an integer.
|
||||||
|
Negative integers are returned unchanged.
|
||||||
|
@ -55,6 +55,9 @@ class HumanizeTests(SimpleTestCase):
|
|||||||
"102",
|
"102",
|
||||||
"103",
|
"103",
|
||||||
"111",
|
"111",
|
||||||
|
"-0",
|
||||||
|
"-1",
|
||||||
|
"-105",
|
||||||
"something else",
|
"something else",
|
||||||
None,
|
None,
|
||||||
)
|
)
|
||||||
@ -70,6 +73,9 @@ class HumanizeTests(SimpleTestCase):
|
|||||||
"102nd",
|
"102nd",
|
||||||
"103rd",
|
"103rd",
|
||||||
"111th",
|
"111th",
|
||||||
|
"0th",
|
||||||
|
"-1",
|
||||||
|
"-105",
|
||||||
"something else",
|
"something else",
|
||||||
None,
|
None,
|
||||||
)
|
)
|
||||||
|
Loading…
Reference in New Issue
Block a user