Date Ordinal Output?

后端 未结 14 1751
囚心锁ツ
囚心锁ツ 2020-11-27 06:38

I\'m wondering if there is a quick and easy way to output ordinals given a number in python.

For example, given the number 1, I\'d like to output

14条回答
  •  无人及你
    2020-11-27 06:54

    Here's a more general solution:

    def ordinal(n):
        if 10 <= n % 100 < 20:
            return str(n) + 'th'
        else:
           return  str(n) + {1 : 'st', 2 : 'nd', 3 : 'rd'}.get(n % 10, "th")
    

提交回复
热议问题