Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Wrote ordinal_digit, all tests pass.

  • Loading branch information...
commit 4e43fb0643b620193b42633b06c7ecbb4d5b5e64 1 parent 2732be8
@lue authored
Showing with 29 additions and 1 deletion.
  1. +29 −1 lib/Lingua/EN/Numbers/Ordinal.pm6
View
30 lib/Lingua/EN/Numbers/Ordinal.pm6
@@ -86,4 +86,32 @@ sub ordinal(Int $input) is export {
}
}
-# TODO: add ordinal_digit [should be millions of times easier :P]
+sub ordinal_digit(Int $input) is export {
+
+ # get the last two digits
+ # The .fmt is to pad single digits to keep [*-2..*-1] from dying
+ my @last_two_nums = $input.Str.fmt('%02d').comb[*-2..*-1]».Int;
+ my $result = $input.Str;
+
+ if @last_two_nums[0] == 1 { # is the last two digits in 10..19?
+ $result ~= "th";
+ }
+ else {
+ given @last_two_nums[1] {
+ when 1 {
+ $result ~= "st";
+ }
+ when 2 {
+ $result ~= "nd";
+ }
+ when 3 {
+ $result ~= "rd";
+ }
+ default {
+ $result ~= "th";
+ }
+ }
+ }
+
+ return $result; # there, that wasn't nearly as hard as &ordinal, was it?
+}
Please sign in to comment.
Something went wrong with that request. Please try again.