Debian Codegraph @

net/sf/saxon/expr/number/Numberer_en.java

raw symbols SCIP
1 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2 // Copyright (c) 2018 Saxonica Limited.
3 // This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
4 // If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
5 // This Source Code Form is "Incompatible With Secondary Licenses", as defined by the Mozilla Public License, v. 2.0.
6 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
7
8 package net.sf.saxon.expr.number;
9
10 /**
11 * Numberer class for the English language.
12 */
13
14 public class Numberer_en extends AbstractNumberer {
15
16 // Separator between tens and units. Allows customisation: "twenty five", "twenty-five", "twentyfive",
17 // or "thirty second", "thirty-second", "thirtysecond".
18 private String tensUnitsSeparatorCardinal = " ";
19
20 // Separator between tens and units. Allows customisation: "thirty second", "thirty-second", "thirtysecond".
21 private String tensUnitsSeparatorOrdinal = "-";
22
23 /**
24 * Set the separator to be used between tens and units for cardinal numbers. This allows customization
25 * of the output, for example "thirty six", "thirty-six", or "thirtysix". Default is a single space.
26 * <p>Currently the only way of calling this is from a subclass, which can be nominated by setting
27 * a {@link net.sf.saxon.lib.LocalizerFactory} on the {@link net.sf.saxon.Configuration}</p>
28 *
29 * @param separator the separator to be used between tens and units when cardinal numbers are written
30 * as words.
31 */
32 public void setTensUnitsSeparatorCardinal(String separator) {
33 tensUnitsSeparatorCardinal = separator;
34 }
35
36 /**
37 * Set the separator to be used between tens and units for ordinal numbers. This allows customization
38 * of the output, for example "thirty sixth", "thirty-sixth", or "thirtysixth". Default is a hyphen.
39 * <p>Currently the only way of calling this is from a subclass, which can be nominated by setting
40 * a {@link net.sf.saxon.lib.LocalizerFactory} on the {@link net.sf.saxon.Configuration}</p>
41 *
42 * @param separator the separator to be used between tens and units when ordinal numbers are written
43 * as words.
44 */
45
46 public void setTensUnitsSeparatorOrdinal(String separator) {
47 tensUnitsSeparatorOrdinal = separator;
48 }
49
50 /**
51 * Set the language used by this numberer. Useful because it can potentially handle variants of English
52 * (and subclasses can handle other languages)
53 *
54 * @param language the requested language. Note that "en-x-hyphen" is recognized as a request to hyphenate
55 * numbers in the range 21-99.
56 */
57
58 public void setLanguage(String language) {
59 super.setLanguage(language);
60 if (language.endsWith("-x-hyphen")) {
61 setTensUnitsSeparatorOrdinal("-");
62 setTensUnitsSeparatorCardinal("-");
63 } else if (language.endsWith("-x-nohyphen")) {
64 setTensUnitsSeparatorOrdinal(" ");
65 setTensUnitsSeparatorCardinal(" ");
66 }
67 }
68
69 /**
70 * Construct the ordinal suffix for a number, for example "st", "nd", "rd"
71 *
72 * @param ordinalParam the value of the ordinal attribute (used in non-English
73 * language implementations)
74 * @param number the number being formatted
75 * @return the ordinal suffix to be appended to the formatted number
76 */
77
78 protected String ordinalSuffix(String ordinalParam, long number) {
79 int penult = (int) (number % 100) / 10;
80 int ult = (int) (number % 10);
81 if (penult == 1) {
82 // e.g. 11th, 12th, 13th
83 return "th";
84 } else {
85 if (ult == 1) {
86 return "st";
87 } else if (ult == 2) {
88 return "nd";
89 } else if (ult == 3) {
90 return "rd";
91 } else {
92 return "th";
93 }
94 }
95 }
96
97 /**
98 * Show the number as words in title case. (We choose title case because
99 * the result can then be converted algorithmically to lower case or upper case).
100 *
101 * @param number the number to be formatted
102 * @return the number formatted as English words
103 */
104
105 public String toWords(long number) {
106 if (number >= 1000000000) {
107 long rem = number % 1000000000;
108 return toWords(number / 1000000000) + " Billion" +
109 (rem == 0 ? "" : (rem < 100 ? " and " : " ") + toWords(rem));
110 } else if (number >= 1000000) {
111 long rem = number % 1000000;
112 return toWords(number / 1000000) + " Million" +
113 (rem == 0 ? "" : (rem < 100 ? " and " : " ") + toWords(rem));
114 } else if (number >= 1000) {
115 long rem = number % 1000;
116 return toWords(number / 1000) + " Thousand" +
117 (rem == 0 ? "" : (rem < 100 ? " and " : " ") + toWords(rem));
118 } else if (number >= 100) {
119 long rem = number % 100;
120 return toWords(number / 100) + " Hundred" +
121 (rem == 0 ? "" : " and " + toWords(rem));
122 } else {
123 if (number < 20) {
124 return englishUnits[(int) number];
125 }
126 int rem = (int) (number % 10);
127 return englishTens[(int) number / 10] +
128 (rem == 0 ? "" : tensUnitsSeparatorCardinal + englishUnits[rem]);
129 }
130 }
131
132 /**
133 * Show an ordinal number as English words in a requested case (for example, Twentyfirst)
134 *
135 * @param ordinalParam the value of the "ordinal" attribute as supplied by the user
136 * @param number the number to be formatted
137 * @param wordCase the required case for example {@link #UPPER_CASE},
138 * {@link #LOWER_CASE}, {@link #TITLE_CASE}
139 * @return the formatted number
140 */
141
142 public String toOrdinalWords(String ordinalParam, long number, int wordCase) {
143 String s;
144 if (number >= 1000000000) {
145 long rem = number % 1000000000;
146 s = toWords(number / 1000000000) + " Billion" +
147 (rem == 0 ? "th" : (rem < 100 ? " and " : " ") +
148 toOrdinalWords(ordinalParam, rem, wordCase));
149 } else if (number >= 1000000) {
150 long rem = number % 1000000;
151 s = toWords(number / 1000000) + " Million" +
152 (rem == 0 ? "th" : (rem < 100 ? " and " : " ") +
153 toOrdinalWords(ordinalParam, rem, wordCase));
154 } else if (number >= 1000) {
155 long rem = number % 1000;
156 s = toWords(number / 1000) + " Thousand" +
157 (rem == 0 ? "th" : (rem < 100 ? " and " : " ") +
158 toOrdinalWords(ordinalParam, rem, wordCase));
159 } else if (number >= 100) {
160 long rem = number % 100;
161 s = toWords(number / 100) + " Hundred" +
162 (rem == 0 ? "th" : " and " +
163 toOrdinalWords(ordinalParam, rem, wordCase));
164 } else {
165 if (number < 20) {
166 s = englishOrdinalUnits[(int) number];
167 } else {
168 int rem = (int) (number % 10);
169 if (rem == 0) {
170 s = englishOrdinalTens[(int) number / 10];
171 } else {
172 s = englishTens[(int) number / 10] + tensUnitsSeparatorOrdinal + englishOrdinalUnits[rem];
173 }
174 }
175 }
176 if (wordCase == UPPER_CASE) {
177 return s.toUpperCase();
178 } else if (wordCase == LOWER_CASE) {
179 return s.toLowerCase();
180 } else {
181 return s;
182 }
183 }
184
185 private static String[] englishUnits = {
186 "Zero", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine",
187 "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen",
188 "Seventeen", "Eighteen", "Nineteen"};
189
190 private static String[] englishTens = {
191 "", "Ten", "Twenty", "Thirty", "Forty", "Fifty",
192 "Sixty", "Seventy", "Eighty", "Ninety"};
193
194 private static String[] englishOrdinalUnits = {
195 "Zeroth", "First", "Second", "Third", "Fourth", "Fifth", "Sixth", "Seventh", "Eighth", "Ninth",
196 "Tenth", "Eleventh", "Twelfth", "Thirteenth", "Fourteenth", "Fifteenth", "Sixteenth",
197 "Seventeenth", "Eighteenth", "Nineteenth"};
198
199 private static String[] englishOrdinalTens = {
200 "", "Tenth", "Twentieth", "Thirtieth", "Fortieth", "Fiftieth",
201 "Sixtieth", "Seventieth", "Eightieth", "Ninetieth"};
202
203
204 /**
205 * Get a month name or abbreviation
206 *
207 * @param month The month number (1=January, 12=December)
208 * @param minWidth The minimum number of characters
209 * @param maxWidth The maximum number of characters
210 */
211
212 public String monthName(int month, int minWidth, int maxWidth) {
213 String name = englishMonths[month - 1];
214 if (maxWidth < 3) {
215 maxWidth = 3;
216 }
217 if (name.length() > maxWidth) {
218 name = name.substring(0, maxWidth);
219 }
220 StringBuilder nameBuilder = new StringBuilder(name);
221 while (nameBuilder.length() < minWidth) {
222 nameBuilder.append(' ');
223 }
224 name = nameBuilder.toString();
225 return name;
226 }
227
228 private static String[] englishMonths = {
229 "January", "February", "March", "April", "May", "June",
230 "July", "August", "September", "October", "November", "December"
231 };
232
233 /**
234 * Get a day name or abbreviation
235 *
236 * @param day The day of the week (1=Monday, 7=Sunday)
237 * @param minWidth The minimum number of characters
238 * @param maxWidth The maximum number of characters
239 */
240
241 public String dayName(int day, int minWidth, int maxWidth) {
242 String name = englishDays[day - 1];
243 if (maxWidth < 2) {
244 maxWidth = 2;
245 }
246 if (name.length() > maxWidth) {
247 name = englishDayAbbreviations[day - 1];
248 if (name.length() > maxWidth) {
249 name = name.substring(0, maxWidth);
250 }
251 }
252 StringBuilder nameBuilder = new StringBuilder(name);
253 while (nameBuilder.length() < minWidth) {
254 nameBuilder.append(' ');
255 }
256 name = nameBuilder.toString();
257 if (minWidth == 1 && maxWidth == 2) {
258 // special case
259 name = name.substring(0, minUniqueDayLength[day - 1]);
260 }
261 return name;
262 }
263
264 private static String[] englishDays = {
265 "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"
266 };
267
268 private static String[] englishDayAbbreviations = {
269 "Mon", "Tues", "Weds", "Thurs", "Fri", "Sat", "Sun"
270 };
271
272 /*@NotNull*/ private static int[] minUniqueDayLength = {
273 1, 2, 1, 2, 1, 2, 2
274 };
275
276
277 }