Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function spellNumber(num) {
- var digitsContent = ["zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"],
- teensContent = ["ten", "eleven", "twelve", "thirteen", "forteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"],
- tensContent = ["twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"],
- num = num.toString(),
- result = new Array();
- // Three digit number
- if (num.length > 2) {
- result.push(digitsContent[parseInt(num[0])]);
- result.push("hundred");
- if (parseInt(num[1]) > 0) {
- // Get a substring of last 2 digits
- if (parseInt((num[1] + num[2])) <= 19) {
- result.push("and");
- result.push(teensContent[parseInt(num[2])]);
- } else {
- result.push(tensContent[parseInt(num[1]) - 2]);
- result.push(digitsContent[parseInt(num[2])]);
- }
- } else {
- result.push("and");
- result.push(digitsContent[parseInt(num[0])]);
- }
- } else {
- if (num.length > 1) {
- // That is 2 digits number
- if (parseInt((num[0] + num[1])) <= 19) {
- result.push(teensContent[parseInt(num[1])]);
- } else {
- result.push(tensContent[parseInt(num[0]) - 2]);
- result.push(digitsContent[parseInt(num[1])]);
- }
- } else {
- // That is just one digit
- result.push(digitsContent[parseInt(num[0])]);
- }
- }
- // Make the first letter capital
- result[0] = result[0].charAt(0).toUpperCase() + result[0].slice(1);
- return result.join(' ');
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement