File Coverage

blib/lib/Lingua/EN/Numbers/Ordinate.pm
Criterion Covered Total %
statement 22 22 100.0
branch 10 10 100.0
condition 11 11 100.0
subroutine 6 6 100.0
pod 2 2 100.0
total 51 51 100.0


line stmt bran cond sub pod time code
1             package Lingua::EN::Numbers::Ordinate;
2             $Lingua::EN::Numbers::Ordinate::VERSION = '1.05';
3             # ABSTRACT: go from cardinal number (3) to ordinal ("3rd")
4              
5 2     2   146673 use 5.006;
  2         17  
6 2     2   12 use strict;
  2         4  
  2         74  
7 2     2   21 use warnings;
  2         21  
  2         627  
8             require Exporter;
9              
10             our @ISA = qw/ Exporter /;
11             our @EXPORT = qw/ ordinate /;
12             our @EXPORT_OK = qw/ ordsuf th /;
13              
14             ###########################################################################
15              
16             =head1 NAME
17              
18             Lingua::EN::Numbers::Ordinate -- go from cardinal number (3) to ordinal ("3rd")
19              
20             =head1 SYNOPSIS
21              
22             use Lingua::EN::Numbers::Ordinate;
23             print ordinate(4), "\n";
24             # prints 4th
25             print ordinate(-342), "\n";
26             # prints -342nd
27              
28             # Example of actual use:
29             ...
30             for(my $i = 0; $i < @records; $i++) {
31             unless(is_valid($record[$i]) {
32             warn "The ", ordinate($i), " record is invalid!\n";
33             next;
34             }
35             ...
36             }
37              
38             =head1 DESCRIPTION
39              
40             There are two kinds of numbers in English -- cardinals (1, 2, 3...), and
41             ordinals (1st, 2nd, 3rd...). This library provides functions for giving
42             the ordinal form of a number, given its cardinal value.
43              
44             =head1 FUNCTIONS
45              
46             =over
47              
48             =item ordinate(SCALAR)
49              
50             Returns a string consisting of that scalar's string form, plus the
51             appropriate ordinal suffix. Example: C returns "23rd".
52              
53             As a special case, C and C return "0th",
54             not "th".
55              
56             This function is exported by default.
57              
58             =item th(SCALAR)
59              
60             Merely an alias for C, but not exported by default.
61              
62             =item ordsuf(SCALAR)
63              
64             Returns just the appropriate ordinal suffix for the given scalar
65             numeric value. This is what C uses to actually do its
66             work. For example, C is "rd".
67              
68             Not exported by default.
69              
70             =back
71              
72             The above functions are all prototyped to take a scalar value,
73             so C is the same as C.
74              
75              
76             =head1 CAVEATS
77              
78             * Note that this library knows only about numbers, not number-words.
79             C might just as well be C
80             or C -- you'll get the fallthru case of the input
81             string plus "th".
82              
83             * As is unavoidable, C returns "174th" (because ordinate
84             sees the value 174). Similarly, C returns
85             "1000000000000th". Returning "trillionth" would be nice, but that's an
86             awfully atypical case.
87              
88             * Note that this library's algorithm (as well as the basic concept
89             and implementation of ordinal numbers) is totally language specific.
90              
91             To pick a trivial example, consider that in French, 1 ordinates
92             as "1ier", whereas 41 ordinates as "41ieme".
93              
94              
95             =head1 SEE ALSO
96              
97             L provides an C function,
98             which returns the ordinal form of a cardinal number.
99              
100             L provides an C
101             function, which returns true if passed an ordinal number.
102              
103             L provides function C
104             which will take a number and return the ordinal as a word.
105             So 3 will result in "third".
106              
107             =head1 REPOSITORY
108              
109             L
110              
111             =head1 COPYRIGHT
112              
113             Copyright (c) 2000 Sean M. Burke. All rights reserved.
114              
115             This library is free software; you can redistribute it and/or
116             modify it under the same terms as Perl itself.
117              
118             =head1 AUTHOR
119              
120             Sean M. Burke C
121              
122             This has been maintained by Neil Bowers (NEILB)
123             since 2014.
124              
125             =cut
126              
127             ###########################################################################
128              
129             sub ordsuf ($) {
130 48 100 100 48 1 8732 return 'th' if not(defined($_[0])) or $_[0] !~ /^-?[0-9]+$/;
131             # 'th' for undef, 0, or anything non-number.
132 43         90 my $n = abs($_[0]); # Throw away the sign.
133              
134 43         86 $n %= 100;
135 43 100 100     208 return 'th' if $n == 11 or $n == 12 or $n == 13;
      100        
136 31         55 $n %= 10;
137 31 100       69 return 'st' if $n == 1;
138 28 100       66 return 'nd' if $n == 2;
139 22 100       63 return 'rd' if $n == 3;
140 13         39 return 'th';
141             }
142              
143             sub ordinate ($) {
144 32   100 32 1 17661 my $i = $_[0] || 0;
145 32         76 return $i . ordsuf($i);
146             }
147              
148 2     2   16 no warnings 'all';
  2         4  
  2         140  
149             *th = \&ordinate; # correctly copies the prototype, too.
150              
151             ###########################################################################
152             1;
153              
154             __END__