Here you can find the source of toEnglish()
/**// w ww .j a va 2s .co m * Extend the Number prototype with a new method called `toEnglish`. * It should return the number as a string using English words. * Examples: * (7).toEnglish(); // > "seven" * (575).toEnglish(); // > "five hundred seventy-five" * (78193512).toEnglish(); // > "seventy-eight million one hundred ninety-three thousand five hundred twelve" * * Extra credit: Make your function support decimals. * Example: * (150043.273).toEnglish() // > "one hundred fifty thousand forty-three and two hundred seventy three thousandths" * */ var numbersToWords = { 0: 'zero', 1: 'one', 2: 'two', 3: 'three', 4: 'four', 5: 'five', 6: 'six', 7: 'seven', 8: 'eight', 9: 'nine', 10: 'ten', 11: 'eleven', 12: 'twelve', 13: 'thirteen', 14: 'fourteen', 15: 'fifteen', 16: 'sixteen', 17: 'seventeen', 18: 'eighteen', 19: 'nineteen', 20: 'twenty', 30: 'thirty', 40: 'forty', 50: 'fifty', 60: 'sixty', 70: 'seventy', 80: 'eighty', 90: 'ninety', }; var numbersToPlace = { 10: 'ten', 100: 'hundred', 1000: 'thousand', 1000000: 'million', 1000000000: 'billion', 1000000000000: 'trillion', 1000000000000000: 'quadrillion', 1000000000000000000: 'quintillion', }; Number.prototype.toEnglish = function () { // return my value as english words var english = ''; var number = this.valueOf(); var divisors = Object.keys(numbersToPlace); var quotient, remainder; if (number < 20) { return numbersToWords[number]; } // Loop through numbersToPlace in reverse order for (var i = divisors.length - 1; i >= 0; i--) { // Divide the number by each item in numbersToPlace quotient = Math.floor(number / divisors[i]); remainder = number % divisors[i]; // If the quotient rounded down to nearest integer is zero, do nothing // But if the quotient is great than zero: if (quotient > 0) { // and the number is below 100, if (number < 100) { return numbersToWords[quotient * 10] + (remainder === 0 ? '' : ('-' + numbersToWords[remainder])); } else { return quotient.toEnglish() + ' ' + numbersToPlace[divisors[i]] + (remainder === 0 ? '' : (' ' + remainder.toEnglish())); } } } return english; };
Number.toHumanSize = function(bytes) { var labels = ['Bytes', 'kB', 'MB', 'GB', 'TB']; if (bytes === 0) { return 'n/a'; var i = parseInt(Math.floor(Math.log(bytes) / Math.log(1024))); return Math.round(bytes / Math.pow(1024, i), 2) + ' ' + labels[i]; };
Number.toHumanTime = function(elapsed) { var labels = ['ms', 's', 'm', 'h', 'd']; var sizes = [1000, 60, 60, 24]; var data = []; sizes.forEach(function(value) { data.push(elapsed % value); elapsed = parseInt(elapsed / value); }); var pos = 0; ...
Number.prototype.humanReadableOrder = function() { if (this>=1000000000) { return (this/1000000000).toFixed(1)+'b'; if (this>=1000000) { return (this/1000000).toFixed(1)+'m'; if (this>=1000) { return (this/1000).toFixed(1)+'k'; ...
Number.prototype.humanReadablePrice = function() { return (this/10000).toFixed(2); };
var numbersToWords = { 0: 'zero', 1: 'one', 2: 'two', 3: 'three', 4: 'four', 5: 'five', 6: 'six', 7: 'seven', ...
Number.prototype.to_kilometers = function() { return this / 1000.0; };
Number.prototype.to_kilometers_per_hour = function() { return this / 1000.0 * 3600.0; };
Number.prototype.to_miles = function() { return this / 1609.344; };
Number.prototype.to_miles_per_hour = function() { return this / 1609.344 * 3600.0; };