如何在Perl中检索整数的序数后缀(如st,nd,rd,th) [英] How do I retrieve an integer's ordinal suffix in Perl (like st, nd, rd, th)

查看:133
本文介绍了如何在Perl中检索整数的序数后缀(如st,nd,rd,th)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有数字,需要添加后缀:'st','nd','rd','th'.因此,例如:如果数字为42,则后缀为'nd',521为'st',113为'th',依此类推. 我需要在perl中执行此操作.任何指针.

I have number and need to add the suffix: 'st', 'nd', 'rd', 'th'. So for example: if the number is 42 the suffix is 'nd' , 521 is 'st' and 113 is 'th' and so on. I need to do this in perl. Any pointers.

推荐答案

尝试一下:

my $ordinal;
if ($foo =~ /(?<!1)1$/) {
    $ordinal = 'st';
} elsif ($foo =~ /(?<!1)2$/) {
    $ordinal = 'nd';
} elsif ($foo =~ /(?<!1)3$/) {
    $ordinal = 'rd';
} else {
    $ordinal = 'th';
}

这篇关于如何在Perl中检索整数的序数后缀(如st,nd,rd,th)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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