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.
相关问题
- $ENV{$variable} in perl
- Improve converting string to readable urls
- Is it possible to pass command-line arguments to @
- Regex to match charset
- Regex subsequence matching
相关文章
- Optimization techniques for backtracking regex imp
- Regex to check for new line
- Allow only 2 decimal points entry to a textbox usi
- Running a perl script on windows without extension
- Comparing speed of non-matching regexp
- Can NOT List directory including space using Perl
- Regular expression to get URL in string swift with
- Extracting columns from text file using Perl one-l
Try this:
Use Lingua::EN::Numbers::Ordinate. From the synopsis:
Another solution (though I like the preexisting answers that are independent of using modules better):
Try this brief subroutine
output
Here's a solution which I originally wrote for a code golf challenge, slightly rewritten to conform to usual best practices for non-golf code:
The way it works is that the regexp
(1?\d)$
matches the last digit of the number, plus the preceding digit if it is1
. The substitution then uses the matched digit(s) as an index to the list(qw'th st nd rd')
, mapping 0 toth
, 1 tost
, 2 tond
, 3 tord
and any other value to undef. Finally, the||
operator replaces undef withth
.If you don't like
s///e
, essentially the same solution could be written e.g. like this:or as a function: