-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEnglishInt.js
More file actions
49 lines (45 loc) · 1.42 KB
/
Copy pathEnglishInt.js
File metadata and controls
49 lines (45 loc) · 1.42 KB
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
// https://leetcode-cn.com/problems/english-int-lcci/
var Test = require('../Common/Test');
var numberToWords = function (num) {
if (num == 0) return "Zero";
const intMap = [
"", "One", "Two", "Three", "Four",
"Five", "Six", "Seven", "Eight", "Nine",
"Ten", "Eleven", "Twelve", "Thirteen", "Fourteen",
"Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen",
"Twenty"
];
const tensMap = ["Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"];
const big = ["", "Thousand", "Million", "Billion"];
let index = 0;
let s = "";
do {
const threeDigits = num % 1000;
if (threeDigits > 0) {
const hundreds = Math.trunc(threeDigits / 100);
const twoDigits = threeDigits % 100;
const rest = twoDigits <= 20 ?
intMap[twoDigits] :
`${tensMap[Math.trunc((twoDigits - 20) / 10)]} ${intMap[twoDigits % 10]}`;
s = `${hundreds > 0 ? intMap[hundreds] + " Hundred " : ""}${rest} ${big[index]} ${s}`;
}
num = Math.trunc(num / 1000);
index++;
} while (num > 0);
return s.replace(/ +/g, ' ').trim();
};
function test(input) {
Test.test(numberToWords, input);
}
// test(3);
// test(30);
// test(13);
// test(23);
// test(53);
// test(123);
// test(1001);
// test(50868);
// test(100000);
test(1000000);
// test(1234567);
// test(1234567891);