-
Notifications
You must be signed in to change notification settings - Fork 77
/
numberToWords.js
261 lines (213 loc) · 7.56 KB
/
numberToWords.js
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
/*!
* Number-To-Words util
* @version v1.2.4
* @link https://github.com/marlun78/number-to-words
* @author Martin Eneqvist (https://github.com/marlun78)
* @contributors Aleksey Pilyugin (https://github.com/pilyugin),Jeremiah Hall (https://github.com/jeremiahrhall),Adriano Melo (https://github.com/adrianomelo),dmrzn (https://github.com/dmrzn)
* @license MIT
*/
(function () {
'use strict';
var root = typeof self == 'object' && self.self === self && self ||
typeof global == 'object' && global.global === global && global ||
this;
// ========== file: /src/maxSafeInteger.js ==========
var MAX_SAFE_INTEGER = 9007199254740991;
// ========== file: /src/isFinite.js ==========
// Simplified https://gist.github.com/marlun78/885eb0021e980c6ce0fb
function isFinite(value) {
return !(typeof value !== 'number' || value !== value || value === Infinity || value === -Infinity);
}
// ========== file: /src/isSafeNumber.js ==========
function isSafeNumber(value) {
return typeof value === 'number' && Math.abs(value) <= MAX_SAFE_INTEGER;
}
// ========== file: /src/makeOrdinal.js ==========
var ENDS_WITH_DOUBLE_ZERO_PATTERN = /(hundred|thousand|(m|b|tr|quadr)illion)$/;
var ENDS_WITH_TEEN_PATTERN = /teen$/;
var ENDS_WITH_Y_PATTERN = /y$/;
var ENDS_WITH_ZERO_THROUGH_TWELVE_PATTERN = /(zero|one|two|three|four|five|six|seven|eight|nine|ten|eleven|twelve)$/;
var ordinalLessThanThirteen = {
zero: 'zeroth',
one: 'first',
two: 'second',
three: 'third',
four: 'fourth',
five: 'fifth',
six: 'sixth',
seven: 'seventh',
eight: 'eighth',
nine: 'ninth',
ten: 'tenth',
eleven: 'eleventh',
twelve: 'twelfth'
};
/**
* Converts a number-word into an ordinal number-word.
* @example makeOrdinal('one') => 'first'
* @param {string} words
* @returns {string}
*/
function makeOrdinal(words) {
// Ends with *00 (100, 1000, etc.) or *teen (13, 14, 15, 16, 17, 18, 19)
if (ENDS_WITH_DOUBLE_ZERO_PATTERN.test(words) || ENDS_WITH_TEEN_PATTERN.test(words)) {
return words + 'th';
}
// Ends with *y (20, 30, 40, 50, 60, 70, 80, 90)
else if (ENDS_WITH_Y_PATTERN.test(words)) {
return words.replace(ENDS_WITH_Y_PATTERN, 'ieth');
}
// Ends with one through twelve
else if (ENDS_WITH_ZERO_THROUGH_TWELVE_PATTERN.test(words)) {
return words.replace(ENDS_WITH_ZERO_THROUGH_TWELVE_PATTERN, replaceWithOrdinalVariant);
}
return words;
}
function replaceWithOrdinalVariant(match, numberWord) {
return ordinalLessThanThirteen[numberWord];
}
// ========== file: /src/toOrdinal.js ==========
/**
* Converts an integer into a string with an ordinal postfix.
* If number is decimal, the decimals will be removed.
* @example toOrdinal(12) => '12th'
* @param {number|string} number
* @returns {string}
*/
function toOrdinal(number) {
var num = parseInt(number, 10);
if (!isFinite(num)) {
throw new TypeError(
'Not a finite number: ' + number + ' (' + typeof number + ')'
);
}
if (!isSafeNumber(num)) {
throw new RangeError(
'Input is not a safe number, it’s either too large or too small.'
);
}
var str = String(num);
var lastTwoDigits = Math.abs(num % 100);
var betweenElevenAndThirteen = lastTwoDigits >= 11 && lastTwoDigits <= 13;
var lastChar = str.charAt(str.length - 1);
return str + (betweenElevenAndThirteen ? 'th'
: lastChar === '1' ? 'st'
: lastChar === '2' ? 'nd'
: lastChar === '3' ? 'rd'
: 'th');
}
// ========== file: /src/toWords.js ==========
var TEN = 10;
var ONE_HUNDRED = 100;
var ONE_THOUSAND = 1000;
var ONE_MILLION = 1000000;
var ONE_BILLION = 1000000000; // 1.000.000.000 (9)
var ONE_TRILLION = 1000000000000; // 1.000.000.000.000 (12)
var ONE_QUADRILLION = 1000000000000000; // 1.000.000.000.000.000 (15)
var MAX = 9007199254740992; // 9.007.199.254.740.992 (15)
var LESS_THAN_TWENTY = [
'zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten',
'eleven', 'twelve', 'thirteen', 'fourteen', 'fifteen', 'sixteen', 'seventeen', 'eighteen', 'nineteen'
];
var TENTHS_LESS_THAN_HUNDRED = [
'zero', 'ten', 'twenty', 'thirty', 'forty', 'fifty', 'sixty', 'seventy', 'eighty', 'ninety'
];
/**
* Converts an integer into words.
* If number is decimal, the decimals will be removed.
* @example toWords(12) => 'twelve'
* @param {number|string} number
* @param {boolean} [asOrdinal] - Deprecated, use toWordsOrdinal() instead!
* @returns {string}
*/
function toWords(number, asOrdinal) {
var words;
var num = parseInt(number, 10);
if (!isFinite(num)) {
throw new TypeError(
'Not a finite number: ' + number + ' (' + typeof number + ')'
);
}
if (!isSafeNumber(num)) {
throw new RangeError(
'Input is not a safe number, it’s either too large or too small.'
);
}
words = generateWords(num);
return asOrdinal ? makeOrdinal(words) : words;
}
function generateWords(number) {
var remainder, word,
words = arguments[1];
// We’re done
if (number === 0) {
return !words ? 'zero' : words.join(' ').replace(/,$/, '');
}
// First run
if (!words) {
words = [];
}
// If negative, prepend “minus”
if (number < 0) {
words.push('minus');
number = Math.abs(number);
}
if (number < 20) {
remainder = 0;
word = LESS_THAN_TWENTY[number];
} else if (number < ONE_HUNDRED) {
remainder = number % TEN;
word = TENTHS_LESS_THAN_HUNDRED[Math.floor(number / TEN)];
// In case of remainder, we need to handle it here to be able to add the “-”
if (remainder) {
word += '-' + LESS_THAN_TWENTY[remainder];
remainder = 0;
}
} else if (number < ONE_THOUSAND) {
remainder = number % ONE_HUNDRED;
word = generateWords(Math.floor(number / ONE_HUNDRED)) + ' hundred';
} else if (number < ONE_MILLION) {
remainder = number % ONE_THOUSAND;
word = generateWords(Math.floor(number / ONE_THOUSAND)) + ' thousand,';
} else if (number < ONE_BILLION) {
remainder = number % ONE_MILLION;
word = generateWords(Math.floor(number / ONE_MILLION)) + ' million,';
} else if (number < ONE_TRILLION) {
remainder = number % ONE_BILLION;
word = generateWords(Math.floor(number / ONE_BILLION)) + ' billion,';
} else if (number < ONE_QUADRILLION) {
remainder = number % ONE_TRILLION;
word = generateWords(Math.floor(number / ONE_TRILLION)) + ' trillion,';
} else if (number <= MAX) {
remainder = number % ONE_QUADRILLION;
word = generateWords(Math.floor(number / ONE_QUADRILLION)) +
' quadrillion,';
}
words.push(word);
return generateWords(remainder, words);
}
// ========== file: /src/toWordsOrdinal.js ==========
/**
* Converts a number into ordinal words.
* @example toWordsOrdinal(12) => 'twelfth'
* @param {number|string} number
* @returns {string}
*/
function toWordsOrdinal(number) {
var words = toWords(number);
return makeOrdinal(words);
}
var numberToWords = {
toOrdinal: toOrdinal,
toWords: toWords,
toWordsOrdinal: toWordsOrdinal
};
if (typeof exports != 'undefined') {
if (typeof module != 'undefined' && module.exports) {
exports = module.exports = numberToWords;
}
exports.numberToWords = numberToWords;
} else {
root.numberToWords = numberToWords;
}
}());