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