summaryrefslogtreecommitdiff
path: root/base/src/main/java/bjc/utils/math/NumberUtils.java
blob: 53446acb776bb8588f9ca1cf20976f498a15048f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
package bjc.utils.math;

/**
 * A variety of functions for doing useful stuff with numbers.
 * 
 * @author EVE
 *
 */
public class NumberUtils {
	/*
	 * @TODO 2/12/18 Ben Culkin :RomanExpansion
	 * 
	 * Use U+305 for large roman numerals, as well as excels 'concise'
	 * numerals (as implemented by roman()).
	 */

	/**
	 * Convert a number into a roman numeral.
	 * 
	 * @param number
	 *        The number to convert.
	 * @param classic
	 *        Whether to use classic roman numerals (use IIII instead of IV,
	 *        and such).
	 * @return The number as a roman numeral.
	 */
	public static String toRoman(long number, boolean classic) {
		StringBuilder work = new StringBuilder();

		long currNumber = number;

		if(currNumber == 0) {
			return "N";
		}

		if(currNumber < 0) {
			currNumber *= -1;

			work.append("-");
		}

		if(currNumber >= 1000) {
			int numM = (int) (currNumber / 1000);
			currNumber = currNumber % 1000;

			for(int i = 0; i < numM; i++) {
				work.append("M");
			}
		}

		if(currNumber >= 900 && !classic) {
			currNumber = currNumber % 900;

			work.append("CM");
		}

		if(currNumber >= 500) {
			currNumber = currNumber % 500;

			work.append("D");
		}

		if(currNumber >= 400 && !classic) {
			currNumber = currNumber % 400;

			work.append("CD");
		}

		if(currNumber >= 100) {
			int numC = (int) (currNumber / 100);
			currNumber = currNumber % 100;

			for(int i = 0; i < numC; i++) {
				work.append("C");
			}
		}

		if(currNumber >= 90 && !classic) {
			currNumber = currNumber % 90;

			work.append("XC");
		}

		if(currNumber >= 50) {
			currNumber = currNumber % 50;

			work.append("L");
		}

		if(currNumber >= 40 && !classic) {
			currNumber = currNumber % 40;

			work.append("XL");
		}

		if(currNumber >= 10) {
			int numX = (int) (currNumber / 10);
			currNumber = currNumber % 10;

			for(int i = 0; i < numX; i++) {
				work.append("X");
			}
		}

		if(currNumber >= 9 && !classic) {
			currNumber = currNumber % 9;

			work.append("IX");
		}

		if(currNumber >= 5) {
			currNumber = currNumber % 5;

			work.append("V");
		}

		if(currNumber >= 4 && !classic) {
			currNumber = currNumber % 4;

			work.append("IV");
		}

		if(currNumber >= 1) {
			int numI = (int) (currNumber / 1);
			currNumber = currNumber % 1;

			for(int i = 0; i < numI; i++) {
				work.append("I");
			}
		}

		return work.toString();
	}

	/**
	 * Convert a number into a cardinal number.
	 * 
	 * @param number
	 *        The number to convert
	 * @return The number as a cardinal.
	 */
	public static String toCardinal(long number) {
		return toCardinal(number, null);
	}

	private static String[] cardinals = new String[] {
			"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven",
			"twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen",
			"twenty",
	};

	/**
	 * Convert a number into a cardinal number.
	 * 
	 * @param number
	 *        The number to convert to a cardinal.
	 * @param custom
	 *        The customizations to use.
	 * @return The number as a cardinal.
	 */
	public static String toCardinal(long number, CardinalState custom) {
		if(custom != null) {
			String res = custom.handleCustom(number);

			if(res != null) return res;
		}

		if(number < 0) return "negative " + toCardinal(number * -1, custom);

		if(number <= 20) return cardinals[(int) number];

		if(number < 100) {
			if(number % 10 == 0) {
				switch((int) number) {
				case 30:
					return "thirty";
				case 40:
					return "forty";
				case 50:
					return "fifty";
				case 60:
					return "sixty";
				case 70:
					return "seventy";
				case 80:
					return "eighty";
				case 90:
					return "ninety";
				default:
					/*
					 * Shouldn't happen.
					 */
					assert (false);
				}
			}

			long numTens = number / 10;
			long numOnes = number % 10;

			return toCardinal(numTens, custom) + "-" + toCardinal(numOnes, custom);
		}

		if(number < 1000) {
			long numHundreds = number / 100;
			long rest = number % 100;

			return toCardinal(numHundreds, custom) + " hundred and " + toCardinal(rest, custom);
		}

		long MILLION = (long) (Math.pow(10, 6));
		if(number < MILLION) {
			long numThousands = number / 1000;
			long rest = number % 1000;

			return toCardinal(numThousands, custom) + " thousand, " + toCardinal(rest, custom);
		}

		long BILLION = (long) (Math.pow(10, 9));
		if(number < BILLION) {
			long numMillions = number / MILLION;
			long rest = number % MILLION;

			return toCardinal(numMillions, custom) + " million, " + toCardinal(rest, custom);
		}

		long TRILLION = (long) (Math.pow(10, 12));
		if(number < TRILLION) {
			long numBillions = number / BILLION;
			long rest = number % BILLION;

			return toCardinal(numBillions, custom) + " billion, " + toCardinal(rest, custom);
		}

		throw new IllegalArgumentException(
				"Numbers greater than or equal to 1 trillion are not supported yet.");
	}

	/**
	 * Convert a number into an ordinal.
	 * 
	 * @param number
	 *        The number to convert to an ordinal.
	 * @return The number as an ordinal.
	 */
	public static String toOrdinal(long number) {
		if(number < 0) {
			return "minus " + toOrdinal(number);
		}

		if(number < 20) {
			switch((int) number) {
			case 0:
				return "zeroth";
			case 1:
				return "first";
			case 2:
				return "second";
			case 3:
				return "third";
			case 4:
				return "fourth";
			case 5:
				return "fifth";
			case 6:
				return "sixth";
			case 7:
				return "seventh";
			case 8:
				return "eighth";
			case 9:
				return "ninth";
			case 10:
				return "tenth";
			case 11:
				return "eleventh";
			case 12:
				return "twelfth";
			case 13:
				return "thirteenth";
			case 14:
				return "fourteenth";
			case 15:
				return "fifteenth";
			case 16:
				return "sixteenth";
			case 17:
				return "seventeenth";
			case 18:
				return "eighteenth";
			case 19:
				return "nineteenth";
			default:
				/*
				 * Shouldn't happen.
				 */
				assert (false);
			}
		}

		if(number < 100) {
			if(number % 10 == 0) {
				switch((int) number) {
				case 20:
					return "twentieth";
				case 30:
					return "thirtieth";
				case 40:
					return "fortieth";
				case 50:
					return "fiftieth";
				case 60:
					return "sixtieth";
				case 70:
					return "seventieth";
				case 80:
					return "eightieth";
				case 90:
					return "ninetieth";
				default:
					throw new IllegalArgumentException(String.format("Illegal number %d", number));
				}
			}

			long numPostfix = number % 10;
			return toCardinal(number - numPostfix) + "-" + toOrdinal(numPostfix);
		}

		long procNum = number % 100;
		long tens = procNum / 10;
		long ones = procNum % 10;

		if(tens == 1) {
			return Long.toString(number) + "th";
		}

		switch((int) ones) {
		case 1:
			return Long.toString(number) + "st";
		case 2:
			return Long.toString(number) + "nd";
		case 3:
			return Long.toString(number) + "rd";
		default:
			return Long.toString(number) + "th";
		}
	}

	private static char[] radixChars = new char[62];
	static {
		int idx = 0;

		for(char i = 0; i < 10; i++) {
			radixChars[idx] = (char) ('0' + i);

			idx += 1;
		}

		for(char i = 0; i < 26; i++) {
			radixChars[idx] = (char) ('A' + i);

			idx += 1;
		}

		for(char i = 0; i < 26; i++) {
			radixChars[idx] = (char) ('a' + i);

			idx += 1;
		}
	}

	/**
	 * Convert a number into a commafied string.
	 * 
	 * @param val
	 *        The number to convert.
	 * @param mincols
	 *        The minimum number of columns to use.
	 * @param padchar
	 *        The padding char to use.
	 * @param commaInterval
	 *        The interval to place commas at.
	 * @param commaChar
	 *        The character to use as a comma
	 * @param signed
	 *        Whether or not to always display a sign
	 * @param radix
	 *        The radix to use
	 * @return The number as a commafied string.
	 */
	public static String toCommaString(long val, int mincols, char padchar, int commaInterval, char commaChar,
			boolean signed, int radix) {
		if(radix > radixChars.length) {
			throw new IllegalArgumentException(
					String.format("Radix %d is larger than largest supported radix %d", radix,
							radixChars.length));
		}

		StringBuilder work = new StringBuilder();

		boolean isNeg = false;
		long currVal = val;
		if(currVal < 0) {
			isNeg = true;
			currVal *= -1;
		}

		if(currVal == 0) {
			work.append(radixChars[0]);
		} else {
			int valCounter = 0;

			while(currVal != 0) {
				valCounter += 1;

				int radDigit = (int) (currVal % radix);
				work.append(radixChars[radDigit]);
				currVal = currVal / radix;

				if(commaInterval != 0 && valCounter % commaInterval == 0) work.append(commaChar);
			}
		}

		if(isNeg)
			work.append("-");
		else if(signed) work.append("+");

		work.reverse();

		/* @TODO Should we have some way to specify how to pad? */
		StringBuilder pad = new StringBuilder();
		
		if(work.length() < mincols) {
			for(int i = work.length(); i < mincols; i++) {
				pad.append(padchar);
			}
		}

		return pad.toString() + work.toString();
	}

	/**
	 * Convert a number to a normal commafied string.
	 * 
	 * @param val
	 *        The value to convert.
	 * @param mincols
	 *        The minimum number of columns.
	 * @param padchar
	 *        The padding char to use.
	 * @param signed
	 *        Whether or not to display the sign.
	 * @param radix
	 *        The radix to use.
	 * @return The number as a normal commafied string.
	 */
	public static String toNormalString(long val, int mincols, char padchar, boolean signed, int radix) {
		return toCommaString(val, mincols, padchar, 0, ',', signed, radix);
	}
}