Ad
Code
Diff
  • const getUnits = number => ['', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine'][number] || ''
    const getTens = (tens, units) => tens === 1
      ? ['ten', 'eleven', 'twelve', 'thirteen', 'fourteen', 'fifteen', 'sixteen', 'seventeen', 'eighteen', 'nineteen'][units]
      : ['', '', 'twenty ', 'thirty ', 'fourty ', 'fifty ', 'sixty ', 'seventy ', 'eighty ', 'ninety '][tens] + getUnits(units)
    const getLargeNumber = number => ['', 'thousand ', 'million ', 'billion ', 'trillion '][number]
    
    const convertThreeDigits = (numeral, more) => {
      const tensNumber = getTens(~~((numeral % 100) / 10), (numeral % 100) % 10)
      const hundredsNumber = getUnits(~~(numeral / 100)) ? getUnits(~~(numeral / 100)) + ' hundred ' : ''
      const and = (hundredsNumber || more) && tensNumber ? 'and ' : ''
    
      return `${hundredsNumber}${and}${tensNumber}`.trim()
    }
    
    const buildText = (remaining, index = 0) => {
      if (remaining < 1000) return `${convertThreeDigits(remaining)} ${getLargeNumber(index)}`
      return `${buildText(~~(remaining / 1000), index + 1)}${convertThreeDigits(remaining % 1000, remaining / 1000)}`
    }
    
    function numToText(numeral) {
      if (numeral === 0) return 'zero'
    
      const text = buildText(Math.abs(numeral)).trim()
      return numeral < 0 ? `minus ${text}` : `${text}`
    }
    
    • const getUnits = number => ['', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine'][number] || ''
    • const getTens = (tens, units) => tens === 1
    • ? ['ten', 'eleven', 'twelve', 'thirteen', 'fourteen', 'fifteen', 'sixteen', 'seventeen', 'eighteen', 'nineteen'][units]
    • : ['', '', 'twenty ', 'thirty ', 'fourty ', 'fifty ', 'sixty ', 'seventy ', 'eighty ', 'ninety '][tens] + getUnits(units)
    • const getLargeNumber = number => ['', 'thousand ', 'million ', 'billion '][number]
    • const getLargeNumber = number => ['', 'thousand ', 'million ', 'billion ', 'trillion '][number]
    • const convertThreeDigits = (numeral, more) => {
    • const tensNumber = getTens(~~((numeral % 100) / 10), (numeral % 100) % 10)
    • const hundredsNumber = getUnits(~~(numeral / 100)) ? getUnits(~~(numeral / 100)) + ' hundred ' : ''
    • const and = (hundredsNumber || more) && tensNumber ? 'and ' : ''
    • return `${hundredsNumber}${and}${tensNumber}`.trim()
    • }
    • const buildText = (remaining, index = 0) => {
    • if (remaining < 1000) return `${convertThreeDigits(remaining)} ${getLargeNumber(index)}`
    • return `${buildText(~~(remaining / 1000), index + 1)}${convertThreeDigits(remaining % 1000, remaining / 1000)}`
    • }
    • function numToText(numeral) {
    • if (numeral === 0) return 'zero'
    • const text = buildText(Math.abs(numeral)).trim()
    • return numeral < 0 ? `minus ${text}` : `${text}`
    • }