Skip to content

Instantly share code, notes, and snippets.

@ForbesLindesay
Created April 26, 2013 14:28
Show Gist options
  • Star 31 You must be signed in to star a gist
  • Fork 6 You must be signed in to fork a gist
  • Save ForbesLindesay/5467742 to your computer and use it in GitHub Desktop.
Save ForbesLindesay/5467742 to your computer and use it in GitHub Desktop.
function humanize(num){
var ones = ['', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine',
'ten', 'eleven', 'twelve', 'thirteen', 'fourteen', 'fifteen', 'sixteen',
'seventeen', 'eighteen', 'nineteen'];
var tens = ['', '', 'twenty', 'thirty', 'forty', 'fifty', 'sixty', 'seventy', 'eighty',
'ninety'];
var numString = num.toString();
if (num < 0) throw new Error('Negative numbers are not supported.');
if (num === 0) return 'zero';
//the case of 1 - 20
if (num < 20) {
return ones[num];
}
if (numString.length === 2) {
return tens[numString[0]] + ' ' + ones[numString[1]];
}
//100 and more
if (numString.length == 3) {
if (numString[1] === '0' && numString[2] === '0')
return ones[numString[0]] + ' hundred';
else
return ones[numString[0]] + ' hundred and ' + convert(+(numString[1] + numString[2]));
}
if (numString.length === 4) {
var end = +(numString[1] + numString[2] + numString[3]);
if (end === 0) return ones[numString[0]] + ' thousand';
if (end < 100) return ones[numString[0]] + ' thousand and ' + convert(end);
return ones[numString[0]] + ' thousand ' + convert(end);
}
}
@victorighalo
Copy link

Thanks for this.

@drfrostongithub
Copy link

Convert isn't defined dude.

@onigor
Copy link

onigor commented Aug 5, 2020

@drfrostongithub
It is a typo or wasn't refactored.
replace convert with humanize.

@drfrostongithub
Copy link

@drfrostongithub
It is a typo or wasn't refactored.
replace convert with humanize.

Got it

@sobczi
Copy link

sobczi commented Nov 14, 2020

Typescript version

export function convertNumberToWordsEN (value: number): string {
  value = Math.floor(value)
  var ones = [
    '',
    'one',
    'two',
    'three',
    'four',
    'five',
    'six',
    'seven',
    'eight',
    'nine',
    'ten',
    'eleven',
    'twelve',
    'thirteen',
    'fourteen',
    'fifteen',
    'sixteen',
    'seventeen',
    'eighteen',
    'nineteen'
  ]
  var tens = [
    '',
    '',
    'twenty',
    'thirty',
    'forty',
    'fifty',
    'sixty',
    'seventy',
    'eighty',
    'ninety'
  ]

  var numString = value.toString()

  if (value < 0) throw new Error('Negative numbers are not supported.')

  if (value === 0) return 'zero'

  //the case of 1 - 20
  if (value < 20) {
    return ones[value]
  }

  if (numString.length === 2) {
    return tens[Number(numString[0])] + ' ' + ones[Number(numString[1])]
  }

  //100 and more
  if (numString.length == 3) {
    if (numString[1] === '0' && numString[2] === '0')
      return ones[Number(numString[0])] + ' hundred'
    else
      return (
        ones[Number(numString[0])] +
        ' hundred and ' +
        convertNumberToWordsEN(+(numString[1] + numString[2]))
      )
  }

  if (numString.length === 4) {
    var end = +(numString[1] + numString[2] + numString[3])
    if (end === 0) return ones[Number(numString[0])] + ' thousand'
    if (end < 100)
      return (
        ones[Number(numString[0])] +
        ' thousand and ' +
        convertNumberToWordsEN(end)
      )
    return (
      ones[Number(numString[0])] + ' thousand ' + convertNumberToWordsEN(end)
    )
  }
  return ''
}

@LearningExcel
Copy link

Its not working here someone can please help me with this Google Sheet.

https://docs.google.com/spreadsheets/d/1ZCrQlBjfMmO9636npMth9ErDr4kSnK8LhKb7JXS2KxU/edit#gid=877179060

@stepdate
Copy link

stepdate commented May 3, 2021

Thank you! I use it for ID numbers, have to re-convert the words to numbers for database selects. You're working on this?

@saifullah0317
Copy link

saifullah0317/NumberToWords-npm-package@4781f2f
Here is the code for upto unlimited numbers, like trillions of trillions

@frdnrdb
Copy link

frdnrdb commented Nov 11, 2022

my five year old wanted me to spell out all these insane numbers, so i wrote a norwegian numbers-to-words converter for us.
the approach is sligthly diffferent; a recursive check for string-length/ zeros.
supports "unlimited" numbers within about 20 lines of code.

english version: https://gist.github.com/frdnrdb/853d7b264912b97baa7e9b9f279b59b3

@pablowbk
Copy link

pablowbk commented Dec 5, 2022

Thanks a lot for he gist, it really help me!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment