Date Ordinal Output?

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

    Except for 1st, 2nd, and 3rd, I think they all just add th... 4th, 5th, 6th, 11th, 21st ... oh, oops ;-)

    I think this might work:

    def ordinal(num):
         ldig = num % 10
         l2dig = (num // 10) % 10
         if l2dig == 1:
             suffix = 'th'
         elif ldig == 1:
             suffix = 'st'
         elif ldig == 2:
             suffix = 'nd'
         elif ldig == 3:
             suffix = 'rd'
         else: 
             suffix = 'th'
         return '%d%s' % (num, suffix)
    

提交回复
热议问题