Date Ordinal Output?

后端 未结 14 1745
囚心锁ツ
囚心锁ツ 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

    Fixed for negative-inputs, based on eric.frederich's nice sol'n (just added abs when using %):

    def ordinal(num):
        return '%d%s' % (num, { 11: 'th', 12: 'th', 13: 'th'}.get(abs(num) % 100, { 1: 'st',2: 'nd',3: 'rd',}.get(abs(num) % 10, 'th')))
    

提交回复
热议问题