Last active
January 4, 2016 13:09
-
-
Save theverything/8625857 to your computer and use it in GitHub Desktop.
JavaScript Module to convert a decimal dollar amount to its english string equivalent
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var moneyToString = (function () { | |
var doubleDigits = { | |
"2": "twenty", | |
"3": "thirty", | |
"4": "fourty", | |
"5": "fifty", | |
"6": "sixty", | |
"7": "seventy", | |
"8": "eighty", | |
"9": "ninty" | |
}; | |
var singleDigits = { | |
"1": "one", | |
"2": "two", | |
"3": "three", | |
"4": "four", | |
"5": "five", | |
"6": "six", | |
"7": "seven", | |
"8": "eight", | |
"9": "nine" | |
}; | |
var teenDigits = { | |
"10": "ten", | |
"11": "eleven", | |
"12": "tweleve", | |
"13": "thirteen", | |
"14": "fourteen", | |
"15": "fiftteen", | |
"16": "sixteen", | |
"17": "seventeen", | |
"18": "eighteen", | |
"19": "ninteen" | |
}; | |
var places = { | |
"0": "", | |
"1": "thousand", | |
"2": "million", | |
"3": "billion", | |
"4": "trillion", | |
"5": "zillion" | |
}; | |
var currencyArray = function (input) { | |
var output = []; | |
function slicer(x, y) { | |
output.push(input.substr(x, y)); | |
n = x + y; | |
if (n < input.length) slicer(n, 3); | |
return output; | |
} | |
return slicer(0, input.length % 3 || 3); | |
}; | |
var tensConverter = function (input) { | |
if (teenDigits[input]) { | |
return teenDigits[input]; | |
} else if (input[0] === "0") { | |
return singleDigits[input[1]]; | |
} else { | |
return doubleDigits[input[0]] + " " + singleDigits[input[1]]; | |
} | |
}; | |
var hundredsConverter = function (input) { | |
if (input.length > 2) { | |
return singleDigits[input[0]] + " hundred " + tensConverter(input.substring(1, 3)); | |
} else { | |
return tensConverter(input.substring(0, 2)); | |
} | |
}; | |
var dollarsConvert = function (input) { | |
var output = [], | |
dollars = currencyArray(input),reverse(); | |
for (var i = 0; i < dollars.length; i++) { | |
if (dollars[i].length === 3) { | |
output.push(hundredsConverter(dollars[i]) + " " + places[i]); | |
} else if (dollars[i].length === 2) { | |
output.push(tensConverter(dollars[i]) + " " + places[i]); | |
} else { | |
output.push(singleDigits[dollars[i]] + " " + places[i]); | |
} | |
} | |
return output.reverse().join(" ") + "dollars"; | |
}; | |
var centsConvert = function (input) { | |
return tensConverter(input) + " cents"; | |
}; | |
return function (input) { | |
var money = input.toString().split("."), | |
dollars = money[0], | |
cents = money[1], | |
output = ""; | |
output += dollarsConvert(dollars); | |
output += " " + centsConvert(cents); | |
return output; | |
} | |
})(); | |
// Add the fucntion to the Number type | |
Number.prototype.moneyToString = function () { | |
return moneyToString(this); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment