-
-
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); | |
} | |
} |
Convert isn't defined dude.
@drfrostongithub
It is a typo or wasn't refactored.
replace convert
with humanize
.
@drfrostongithub
It is a typo or wasn't refactored.
replaceconvert
withhumanize
.
Got it
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 ''
}
Its not working here someone can please help me with this Google Sheet.
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/NumberToWords-npm-package@4781f2f
Here is the code for upto unlimited numbers, like trillions of trillions
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
Thanks a lot for he gist, it really help me!
Thanks for this.