Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions solution/0200-0299/0273.Integer to English Words/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,58 @@ public class Solution {
}
```

#### TypeScript

```ts
function numberToWords(num: number): string {
if (num === 0) return 'Zero';

// prettier-ignore
const f = (x: number): string => {
const dict1 = ['','One','Two','Three','Four','Five','Six','Seven','Eight','Nine','Ten','Eleven','Twelve','Thirteen','Fourteen','Fifteen','Sixteen','Seventeen','Eighteen','Nineteen',]
const dict2 = ['','','Twenty','Thirty','Forty','Fifty','Sixty','Seventy','Eighty','Ninety',]
let ans = ''

if (x <= 19) ans = dict1[x] ?? ''
else if (x < 100) ans = `${dict2[Math.floor(x / 10)]} ${f(x % 10)}`
else if (x < 10 ** 3) ans = `${dict1[Math.floor(x / 100)]} Hundred ${f(x % 100)}`
else if (x < 10 ** 6) ans = `${f(Math.floor(x / 10 ** 3))} Thousand ${f(x % 10 ** 3)}`
else if (x < 10 ** 9) ans = `${f(Math.floor(x / 10 ** 6))} Million ${f(x % 10 ** 6)}`
else ans = `${f(Math.floor(x / 10 ** 9))} Billion ${f(x % 10 ** 9)}`

return ans.trim()
}

return f(num);
}
```

#### JavaScript

```js
function numberToWords(num) {
if (num === 0) return 'Zero';

// prettier-ignore
const f = (x) => {
const dict1 = ['','One','Two','Three','Four','Five','Six','Seven','Eight','Nine','Ten','Eleven','Twelve','Thirteen','Fourteen','Fifteen','Sixteen','Seventeen','Eighteen','Nineteen',]
const dict2 = ['','','Twenty','Thirty','Forty','Fifty','Sixty','Seventy','Eighty','Ninety',]
let ans = ''

if (x <= 19) ans = dict1[x] ?? ''
else if (x < 100) ans = `${dict2[Math.floor(x / 10)]} ${f(x % 10)}`
else if (x < 10 ** 3) ans = `${dict1[Math.floor(x / 100)]} Hundred ${f(x % 100)}`
else if (x < 10 ** 6) ans = `${f(Math.floor(x / 10 ** 3))} Thousand ${f(x % 10 ** 3)}`
else if (x < 10 ** 9) ans = `${f(Math.floor(x / 10 ** 6))} Million ${f(x % 10 ** 6)}`
else ans = `${f(Math.floor(x / 10 ** 9))} Billion ${f(x % 10 ** 9)}`

return ans.trim()
}

return f(num);
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
52 changes: 52 additions & 0 deletions solution/0200-0299/0273.Integer to English Words/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,58 @@ public class Solution {
}
```

#### TypeScript

```ts
function numberToWords(num: number): string {
if (num === 0) return 'Zero';

// prettier-ignore
const f = (x: number): string => {
const dict1 = ['','One','Two','Three','Four','Five','Six','Seven','Eight','Nine','Ten','Eleven','Twelve','Thirteen','Fourteen','Fifteen','Sixteen','Seventeen','Eighteen','Nineteen',]
const dict2 = ['','','Twenty','Thirty','Forty','Fifty','Sixty','Seventy','Eighty','Ninety',]
let ans = ''

if (x <= 19) ans = dict1[x] ?? ''
else if (x < 100) ans = `${dict2[Math.floor(x / 10)]} ${f(x % 10)}`
else if (x < 10 ** 3) ans = `${dict1[Math.floor(x / 100)]} Hundred ${f(x % 100)}`
else if (x < 10 ** 6) ans = `${f(Math.floor(x / 10 ** 3))} Thousand ${f(x % 10 ** 3)}`
else if (x < 10 ** 9) ans = `${f(Math.floor(x / 10 ** 6))} Million ${f(x % 10 ** 6)}`
else ans = `${f(Math.floor(x / 10 ** 9))} Billion ${f(x % 10 ** 9)}`

return ans.trim()
}

return f(num);
}
```

#### JavaScript

```js
function numberToWords(num) {
if (num === 0) return 'Zero';

// prettier-ignore
const f = (x) => {
const dict1 = ['','One','Two','Three','Four','Five','Six','Seven','Eight','Nine','Ten','Eleven','Twelve','Thirteen','Fourteen','Fifteen','Sixteen','Seventeen','Eighteen','Nineteen',]
const dict2 = ['','','Twenty','Thirty','Forty','Fifty','Sixty','Seventy','Eighty','Ninety',]
let ans = ''

if (x <= 19) ans = dict1[x] ?? ''
else if (x < 100) ans = `${dict2[Math.floor(x / 10)]} ${f(x % 10)}`
else if (x < 10 ** 3) ans = `${dict1[Math.floor(x / 100)]} Hundred ${f(x % 100)}`
else if (x < 10 ** 6) ans = `${f(Math.floor(x / 10 ** 3))} Thousand ${f(x % 10 ** 3)}`
else if (x < 10 ** 9) ans = `${f(Math.floor(x / 10 ** 6))} Million ${f(x % 10 ** 6)}`
else ans = `${f(Math.floor(x / 10 ** 9))} Billion ${f(x % 10 ** 9)}`

return ans.trim()
}

return f(num);
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
21 changes: 21 additions & 0 deletions solution/0200-0299/0273.Integer to English Words/Solution.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
function numberToWords(num) {
if (num === 0) return 'Zero';

// prettier-ignore
const f = (x) => {
const dict1 = ['','One','Two','Three','Four','Five','Six','Seven','Eight','Nine','Ten','Eleven','Twelve','Thirteen','Fourteen','Fifteen','Sixteen','Seventeen','Eighteen','Nineteen',]
const dict2 = ['','','Twenty','Thirty','Forty','Fifty','Sixty','Seventy','Eighty','Ninety',]
let ans = ''

if (x <= 19) ans = dict1[x] ?? ''
else if (x < 100) ans = `${dict2[Math.floor(x / 10)]} ${f(x % 10)}`
else if (x < 10 ** 3) ans = `${dict1[Math.floor(x / 100)]} Hundred ${f(x % 100)}`
else if (x < 10 ** 6) ans = `${f(Math.floor(x / 10 ** 3))} Thousand ${f(x % 10 ** 3)}`
else if (x < 10 ** 9) ans = `${f(Math.floor(x / 10 ** 6))} Million ${f(x % 10 ** 6)}`
else ans = `${f(Math.floor(x / 10 ** 9))} Billion ${f(x % 10 ** 9)}`

return ans.trim()
}

return f(num);
}
21 changes: 21 additions & 0 deletions solution/0200-0299/0273.Integer to English Words/Solution.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
function numberToWords(num: number): string {
if (num === 0) return 'Zero';

// prettier-ignore
const f = (x: number): string => {
const dict1 = ['','One','Two','Three','Four','Five','Six','Seven','Eight','Nine','Ten','Eleven','Twelve','Thirteen','Fourteen','Fifteen','Sixteen','Seventeen','Eighteen','Nineteen',]
const dict2 = ['','','Twenty','Thirty','Forty','Fifty','Sixty','Seventy','Eighty','Ninety',]
let ans = ''

if (x <= 19) ans = dict1[x] ?? ''
else if (x < 100) ans = `${dict2[Math.floor(x / 10)]} ${f(x % 10)}`
else if (x < 10 ** 3) ans = `${dict1[Math.floor(x / 100)]} Hundred ${f(x % 100)}`
else if (x < 10 ** 6) ans = `${f(Math.floor(x / 10 ** 3))} Thousand ${f(x % 10 ** 3)}`
else if (x < 10 ** 9) ans = `${f(Math.floor(x / 10 ** 6))} Million ${f(x % 10 ** 6)}`
else ans = `${f(Math.floor(x / 10 ** 9))} Billion ${f(x % 10 ** 9)}`

return ans.trim()
}

return f(num);
}