]> www.ginac.de Git - cln.git/blob - src/real/format-output/cl_fmt_ordinal.cc
* All Files have been modified for inclusion of namespace cln;
[cln.git] / src / real / format-output / cl_fmt_ordinal.cc
1 // format_ordinal().
2
3 // General includes.
4 #include "cl_sysdep.h"
5
6 // Specification.
7 #include "cl_format.h"
8
9
10 // Implementation.
11
12 #include "cln/integer.h"
13
14 namespace cln {
15
16 static const char * const cl_format_ordinal_ones [20] = {
17         NULL,
18         "first",
19         "second",
20         "third",
21         "fourth",
22         "fifth",
23         "sixth",
24         "seventh",
25         "eighth",
26         "ninth",
27         "tenth",
28         "eleventh",
29         "twelfth",
30         "thirteenth",
31         "fourteenth",
32         "fifteenth",
33         "sixteenth",
34         "seventeenth",
35         "eighteenth",
36         "nineteenth",
37 };
38
39 static const char * const cl_format_ordinal_tens [10] = {
40         NULL,
41         "tenth",
42         "twentieth",
43         "thirtieth",
44         "fortieth",
45         "fiftieth",
46         "sixtieth",
47         "seventieth",
48         "eightieth",
49         "ninetieth",
50 };
51
52 void format_ordinal (cl_ostream stream, const cl_I& argument)
53 {
54         if (zerop(argument))
55                 fprint(stream,"zeroth");
56         else {
57                 var cl_I arg = argument;
58                 if (minusp(arg)) {
59                         fprint(stream,"minus ");
60                         arg = -arg;
61                 }
62                 var cl_I_div_t div = floor2(arg,100);
63                 var const cl_I& hundreds = div.quotient;
64                 var uintL tens_and_ones = cl_I_to_UL(div.remainder);
65                 if (hundreds > 0)
66                         format_cardinal(stream,hundreds*100);
67                 if (tens_and_ones == 0)
68                         fprint(stream,"th");
69                 else {
70                         var uintL tens = floor(tens_and_ones,10);
71                         var uintL ones = tens_and_ones % 10;
72                         if (hundreds > 0)
73                                 fprintchar(stream,' ');
74                         if (tens < 2)
75                                 fprint(stream,cl_format_ordinal_ones[tens_and_ones]);
76                         elif (ones == 0)
77                                 fprint(stream,cl_format_ordinal_tens[tens]);
78                         else {
79                                 fprint(stream,cl_format_tens[tens]);
80                                 fprintchar(stream,'-');
81                                 fprint(stream,cl_format_ordinal_ones[ones]);
82                         }
83                 }
84         }
85 }
86
87 }  // namespace cln