Created
February 23, 2025 00:32
-
-
Save hsingtism/e1f464b1513c098466d4eb44d105efa6 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const ones = ['', 'one ', 'two ', 'three ', 'four ', 'five ', 'six ', 'seven ', 'eight ', 'nine ', 'ten ', 'eleven ', 'twelve ', 'thirteen ', 'fourteen ', 'fifteen ', 'sixteen ', 'seventeen ', 'eighteen ', 'nineteen '] | |
| const tens = ['', 'ten ', 'twenty ', 'thirty ', 'fourty ', 'fifty ', 'sixty ', 'seventy ', 'eighty ', 'ninety '] | |
| const exps = (v, i) => v + (v ? ['quadrillion ', 'trillion ', 'billion ', 'million ', 'thousand ', ''][i] : '') | |
| const subthousands = n => n == 0 ? '' : | |
| n > 99 ? subthousands(~~(n / 100)) + 'hundred ' + (n % 100 ? 'and ' : '') + subthousands(n % 100) : | |
| n > 19 ? tens[~~(n / 10)] + subthousands(n % 10) : ones[n] | |
| const numToWord = n => | |
| (n < 0 || !Number.isSafeInteger(n)) ? null : n === 0 ? 'zero ' : | |
| String(n).padStart(18, '0').match(/.{1,3}/g).map(Number).map(subthousands).map(exps).join('') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment