-
Notifications
You must be signed in to change notification settings - Fork 0
/
numberToText.js
98 lines (87 loc) · 2.03 KB
/
numberToText.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
function numberToText(numberString) {
if (isNaN(numberString)) {
return numberString + " is not a number. Please enter a number.";
}
var output = "";
const length = numberString.length;
const ones = [
"",
"One",
"Two",
"Three",
"Four",
"Five",
"Six",
"Seven",
"Eight",
"Nine",
"Ten",
"Eleven",
"Twelve",
"Thirteen",
"Fourteen",
"Fifteen",
"Sixteen",
"Seventeen",
"Eighteen",
"Nineteen",
];
const tens = [
"",
"",
"Twenty",
"Thirty",
"Forty",
"Fifty",
"Sixty",
"Seventy",
"Eighty",
"Ninety",
];
const hundreds = [
"",
"Thousand",
"Million",
"Billion",
"Trillion",
"Quadrillion",
"Quintillion",
];
if (parseInt(numberString) < 1) {
return "Zero";
}
var i = 0;
do {
var subStringConverted = "";
let substring = numberString.slice(-3);
if (parseInt(substring) > 0) {
if (substring.length == 3 && substring[0] != "0") {
subStringConverted += ones[parseInt(substring.charAt(0))] + " Hundred ";
}
if (
parseInt(substring.slice(-2)) < 20 &&
parseInt(substring.slice(-2)) > 0
) {
subStringConverted += ones[parseInt(substring.slice(-2))];
} else {
subStringConverted +=
tens[parseInt(substring.charAt(1))] +
" " +
ones[parseInt(substring.charAt(2))];
}
subStringConverted += " " + hundreds[i] + " ";
}
numberString = numberString.slice(0, numberString.length - 3);
output = subStringConverted + output;
i++;
} while (i * 3 < length);
return output;
}
console.log(numberToText("123456789"));
console.log(numberToText("333333"));
console.log(numberToText("0"));
console.log(numberToText("12"));
console.log(numberToText("307"));
console.log(numberToText("1000234"));
console.log(numberToText("123456789123456789"));
console.log(numberToText("testing"));