日期顺序输出? [英] Date Ordinal Output?

查看:29
本文介绍了日期顺序输出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有一种快速简便的方法可以在 python 中输出给定数字的序数.

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

例如,给定数字1,我想输出"1st",数字2,"第二个",等等,等等.

For example, given the number 1, I'd like to output "1st", the number 2, "2nd", et cetera, et cetera.

这用于在面包屑路径中处理日期

This is for working with dates in a breadcrumb trail

Home >  Venues >  Bar Academy >  2009 >  April >  01 

是当前显示的内容

我想要一些类似的东西

Home >  Venues >  Bar Academy >  2009 >  April >  1st

推荐答案

或者缩短 David 的回答:

Or shorten David's answer with:

if 4 <= day <= 20 or 24 <= day <= 30:
    suffix = "th"
else:
    suffix = ["st", "nd", "rd"][day % 10 - 1]

这篇关于日期顺序输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆