Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 44x 3x 3x 41x 44x 19x 19x 22x 22x 22x 22x 22x 44x 7x 7x 7x 22x 22x 22x 44x 11x 11x 11x 22x 22x 22x 44x 5x 44x 5x 17x 12x 12x 12x 12x 11x 11x 12x 22x 22x 22x | const ONES = [
'zero',
'one',
'two',
'three',
'four',
'five',
'six',
'seven',
'eight',
'nine',
'ten',
'eleven',
'twelve',
'thirteen',
'fourteen',
'fifteen',
'sixteen',
'seventeen',
'eighteen',
'nineteen',
'twenty',
]
const TENS = ['', '', 'twenty', 'thirty', 'forty', 'fifty', 'sixty', 'seventy', 'eighty', 'ninety']
/**
* Convert a number (0-9999) to English words.
*
* Examples:
* numberToEnglish(5) → "five"
* numberToEnglish(42) → "forty two"
* numberToEnglish(157) → "one hundred fifty seven"
* numberToEnglish(2345) → "two thousand three hundred forty five"
*/
export function numberToEnglish(n: number): string {
if (n < 0 || n > 9999 || !Number.isInteger(n)) {
throw new Error(`numberToEnglish: expected integer 0-9999, got ${n}`)
}
if (n <= 20) {
return ONES[n]
}
const parts: string[] = []
// Thousands
const thousands = Math.floor(n / 1000)
if (thousands > 0) {
parts.push(ONES[thousands])
parts.push('thousand')
}
// Hundreds
const hundreds = Math.floor((n % 1000) / 100)
if (hundreds > 0) {
parts.push(ONES[hundreds])
parts.push('hundred')
}
// Remainder (0-99)
const remainder = n % 100
if (remainder === 0) {
// Nothing to add
} else if (remainder <= 20) {
parts.push(ONES[remainder])
} else {
const tens = Math.floor(remainder / 10)
const ones = remainder % 10
parts.push(TENS[tens])
if (ones > 0) {
parts.push(ONES[ones])
}
}
return parts.join(' ')
}
|