Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I retrieve an integer's ordinal suffix in Perl (like st, nd, rd, th)

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.

like image 910
bozo user Avatar asked Jul 06 '12 21:07

bozo user


1 Answers

Use Lingua::EN::Numbers::Ordinate. From the synopsis:

use Lingua::EN::Numbers::Ordinate;
print ordinate(4), "\n";
 # prints 4th
print ordinate(-342), "\n";
 # prints -342nd

# Example of actual use:
...
for(my $i = 0; $i < @records; $i++) {
  unless(is_valid($record[$i]) {
    warn "The ", ordinate($i), " record is invalid!\n"; 
    next;
  }
  ...
}
like image 110
Bill Ruppert Avatar answered Oct 06 '22 13:10

Bill Ruppert