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, ', 'm', 'b', 'tr', 'quadr', 'quint', 'sext', 'sept', 'oct', 'non'][number]
    
    const getVeryLargeNumber = index => {
      if (index < 10) return getLargeNumber(index)
    
      const mappedIndex = (index - 9) / 3
      const prefix = ['', 'un', 'duo', 'tre', 'quattor', 'quin', 'sex', 'septen', 'octo', 'novem'][mappedIndex % 10]
      const suffix = ['dec', 'vigint', 'trigint', 'quadragint', 'quinquagint', 'sexagint', 'septuagint', 'octogint', 'nonagint'][~~(mappedIndex / 10)]
      return `${prefix}${suffix}`
    }
    
    const buildThreeDigits = (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) => {
      const largeNumber = `${index > 1 ? getLargeNumber(index) + 'illion, ' : getLargeNumber(index)}`
      if (remaining < 1000) return `${buildThreeDigits(remaining)} ${largeNumber}`
    
      const layer = buildThreeDigits(remaining % 1000, index === 0)
      return `${buildText(Math.floor(remaining / 1000), index + 1)}${layer ? `${layer} ${largeNumber}` : ''}`
    }
    
    const buildLargeNumber = (numeral) => {
      const index = ~~(Math.log10(numeral) / 3)
      const topNumber = Math.round(numeral / Math.pow(10, index * 3))
      const topThree = buildThreeDigits(topNumber)
    
      return `${topThree} ${getVeryLargeNumber(index)}illion`
    }
    
    function numToText(numeral) {
      if (numeral === 0) return 'zero'
    
      const text = numeral <= Number.MAX_SAFE_INTEGER
        ? buildText(Math.abs(numeral)).replace(/[\s,]*$/, '')
        : buildLargeNumber(Math.abs(numeral))
    
      return numeral < 0 ? `minus ${text}` : `${text}`
    }
    
    • const getUnits = number => ['', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine'][number] || ''
    • 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 getLargeNumber = number => ['', 'thousand, ', 'm', 'b', 'tr', 'quadr', 'quint', 'sext', 'sept', 'oct', 'non'][number]
    • const convertThreeDigits = (numeral, more) => {
    • const getVeryLargeNumber = index => {
    • if (index < 10) return getLargeNumber(index)
    • const mappedIndex = (index - 9) / 3
    • const prefix = ['', 'un', 'duo', 'tre', 'quattor', 'quin', 'sex', 'septen', 'octo', 'novem'][mappedIndex % 10]
    • const suffix = ['dec', 'vigint', 'trigint', 'quadragint', 'quinquagint', 'sexagint', 'septuagint', 'octogint', 'nonagint'][~~(mappedIndex / 10)]
    • return `${prefix}${suffix}`
    • }
    • const buildThreeDigits = (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)}`
    • const largeNumber = `${index > 1 ? getLargeNumber(index) + 'illion, ' : getLargeNumber(index)}`
    • if (remaining < 1000) return `${buildThreeDigits(remaining)} ${largeNumber}`
    • const layer = buildThreeDigits(remaining % 1000, index === 0)
    • return `${buildText(Math.floor(remaining / 1000), index + 1)}${layer ? `${layer} ${largeNumber}` : ''}`
    • }
    • const buildLargeNumber = (numeral) => {
    • const index = ~~(Math.log10(numeral) / 3)
    • const topNumber = Math.round(numeral / Math.pow(10, index * 3))
    • const topThree = buildThreeDigits(topNumber)
    • return `${topThree} ${getVeryLargeNumber(index)}illion`
    • }
    • function numToText(numeral) {
    • if (numeral === 0) return 'zero'
    • const text = buildText(Math.abs(numeral)).trim()
    • const text = numeral <= Number.MAX_SAFE_INTEGER
    • ? buildText(Math.abs(numeral)).replace(/[\s,]*$/, '')
    • : buildLargeNumber(Math.abs(numeral))
    • return numeral < 0 ? `minus ${text}` : `${text}`
    • }

Given any number (let's say up to the billions). It should convert that number into an English text string.

See the test cases for more information

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 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}`
}