Node.js examples for Number:Format
Simple PHP style number formatting
/**//from w w w. j ava2s . co m * Simple PHP style number formatting * * Does not round * * @see http://php.net/number_format * @param {Number} precision Number of digits to display after the decimal, default 0 * @param {String} decimal Character to use as the decimal, default '.' * @param {String} separator Character to use as the thousands separator, default ',' * @return {String} The formatted string */ Number.prototype.format = function(precision, decimal, separator) { if (!precision) {precision = 0;} if (!decimal) {decimal = '.';} if (!separator) {separator = ',';} var string_this = String(this); var x = string_this.split('.'); var formatted_number = x[0]; var remainder = x.length > 1 ? x[1] : '0'; var regex = /(\d+)(\d{3})/; while (regex.test(formatted_number)) { formatted_number = formatted_number.replace(regex, '$1'+separator+'$2'); } if (precision > 0) { formatted_number += decimal; for (var a = 0; a < precision; a++) { if (remainder[a]) { formatted_number += remainder[a]; } else { formatted_number += '0'; } } } return formatted_number; }; }();