List of utility methods to do Number Format
format(n, x, s, c)Number.prototype.format = function(n, x, s, c) { var re = '\\d(?=(\\d{' + (x || 3) + '})+' + (n > 0 ? '\\D' : '$') + ')', num = this.toFixed(Math.max(0, ~~n)); return (c ? num.replace('.', c) : num).replace(new RegExp(re, 'g'), '$&' + (s || ',')); }; | |
format(n, x, s, c)Number.prototype.format = function(n, x, s, c) { var re = '\\d(?=(\\d{' + (x || 3) + '})+' + (n > 0 ? '\\D' : '$') + ')' , num = this.toFixed(Math.max(0, ~~n)); return (c ? num.replace('.', c) : num).replace(new RegExp(re, 'g'), '$&' + (s || ',')); }; | |
format(n, x, s, c)Number.prototype.format = function(n, x, s, c) { if (arguments.length === 0) { n = 2; x = 3; s = ','; c = '.'; } var re = '\\d(?=(\\d{' + (x || 3) + '})+' + (n > 0 ? '\\D' : '$') + ')', num = this.toFixed(Math.max(0, Math.floor(n))); return (c ? num.replace('.', c) : num).replace(new RegExp(re, 'g'), '$&' + (s || ',')); }; | |
format(n, x, unit)Number.prototype.format = function(n, x, unit) { var re = '\\d(?=(\\d{' + (x || 3) + '})+' + (n > 0 ? '\\,' : '$') + ')'; var concatunit = ""; if (unit != null){ concatunit = " "+unit; } return this.toFixed(Math.max(0, ~~n)).replace(new RegExp(re, 'g'), '$&.')+concatunit; | |
formatNum(n, x, s, c)Number.prototype.formatNum = function(n, x, s, c) { var re = '\\d(?=(\\d{' + (x || 3) + '})+' + (n > 0 ? '\\D' : '$') + ')', num = this.toFixed(Math.max(0, ~~n)); return (c ? num.replace('.', c) : num).replace(new RegExp(re, 'g'), '$&' + (s || ',')); }; | |
formatNumber()Number.prototype.formatNumber = function() { var a = Math.abs(this).toString().split(''); v = ''; var n = 0; while (a.length > 0) { if (n>0 && n%3 == 0) v = '.' + v; v = a.pop() + v; n ++; ... | |
formatNumber(c, d, t)Number.prototype.formatNumber = function(c, d, t){ var n = this, c = isNaN(c = Math.abs(c)) ? 2 : c, d = d == undefined ? "." : d, t = t == undefined ? "," : t, s = n < 0 ? "-" : "", i = parseInt(n = Math.abs(+n || 0).toFixed(c)) + "", j = (j = i.length) > 3 ? j % 3 : 0; return s + (j ? i.substr(0, j) + t : "") + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + t) + (c ? d + Math.abs(n - i).toFixed(c).slice(2) : ""); ... | |
formatNumber(c, d, t)Number.prototype.formatNumber = function(c, d, t) { var n = this, c = isNaN(c = Math.abs(c)) ? 2 : c, d = d == undefined ? "," : d, t = t == undefined ? "." : t, s = n < 0 ? "-" : "", i = parseInt(n = Math.abs(+n || 0).toFixed(c)) + "", j = (j = i.length) > 3 ? j % 3 : 0; return s + (j ? i.substr(0, j) + t : "") + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + t) + (c ? d + Math.abs(n - i).toFixed(c).slice(2) : ""); | |
format(thousandSep, decPoint)Number.THOUSAND_STEP = ","; Number.DECIMAL_POINT = "."; Number.prototype.format = function(thousandSep, decPoint) { var decimal, str; thousandSep = thousandSep || Number.THOUSAND_STEP; decPoint = decPoint || Number.DECIMAL_POINT; decimal = this.toString().split("."); str = Math.abs(this) .toFixed(0) ... | |
formatAmount(c, d, t)Number.prototype.formatAmount=function(c, d, t) { var n = this, c = isNaN(c = Math.abs(c)) ? 2 : c, d = d == undefined ? "." : d, t = t == undefined ? "," : t, s = n < 0 ? "-" : "", i = parseInt(n = Math.abs(+n || 0).toFixed(c)) + "", j = (j = i.length) > 3 ? j % 3 : 0; return s + "?" + (j ? i.substr(0, j) + t : "") + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + t) + (c ? d + Math.abs(n - i).toFixed(c).slice(2) : ""); ... |