Skip to content

Instantly share code, notes, and snippets.

@hsingtism
Created February 23, 2025 00:32
Show Gist options
  • Select an option

  • Save hsingtism/e1f464b1513c098466d4eb44d105efa6 to your computer and use it in GitHub Desktop.

Select an option

Save hsingtism/e1f464b1513c098466d4eb44d105efa6 to your computer and use it in GitHub Desktop.
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