5个缺失的 JavaScript 数字格式化函数

时间:2024-01-03 22:47:32
/** 下面两个函数都能对浮点数进行四舍五入,保留小数点后两位 **/

function CurrencyFormatted(amount) {
var i = parseFloat(amount);
if(isNaN(i)) { i = 0.00; }
var minus = '';
if(i < 0) { minus = '-'; }
i = Math.abs(i);
i = parseInt((i + .005) * 100);
i = i / 100;
s = new String(i);
if(s.indexOf('.') < 0) { s += '.00'; }
if(s.indexOf('.') == (s.length - 2)) { s += '0'; }
s = minus + s;
return s;
} /**
* Usage: CurrencyFormatted(12345.678);
* result: 12345.68
**/ function format_number(pnumber,decimals){
if (isNaN(pnumber)) { return 0};
if (pnumber=='') { return 0}; var snum = new String(pnumber);
var sec = snum.split('.');
var whole = parseFloat(sec[0]);
var result = ''; if(sec.length > 1){
var dec = new String(sec[1]);
dec = String(parseFloat(sec[1])/Math.pow(10,(dec.length - decimals)));
dec = String(whole + Math.round(parseFloat(dec))/Math.pow(10,decimals));
var dot = dec.indexOf('.');
if(dot == -1){
dec += '.';
dot = dec.indexOf('.');
}
while(dec.length <= dot + decimals) { dec += '0'; }
result = dec;
} else{
var dot;
var dec = new String(whole);
dec += '.';
dot = dec.indexOf('.');
while(dec.length <= dot + decimals) { dec += '0'; }
result = dec;
}
return result;
} /**
* Usage: format_number(12345.678, 2);
* result: 12345.68
**/

  

/** 每三位数字添加一个逗号,方便阅读 **/

function CommaFormatted(amount) {
var delimiter = ","; // replace comma if desired
amount = new String(amount);
var a = amount.split('.',)
var d = a[];
var i = parseInt(a[]);
if(isNaN(i)) { return ''; }
var minus = '';
if(i < ) { minus = '-'; }
i = Math.abs(i);
var n = new String(i);
var a = [];
while(n.length > )
{
var nn = n.substr(n.length-);
a.unshift(nn);
n = n.substr(,n.length-);
}
if(n.length > ) { a.unshift(n); }
n = a.join(delimiter);
if(d.length < ) { amount = n; }
else { amount = n + '.' + d; }
amount = minus + amount;
return amount;
} /**
* Usage: CommaFormatted(12345678);
* result: 12,345,678
**/ function addCommas(nStr) {
nStr += '';
var x = nStr.split('.');
var x1 = x[];
var x2 = x.length >; ? '.' + x[] : '';
var rgx = /(d+)(d{})/; while (rgx.test(x1)) {
x1 = x1.replace(rgx, '$1' + ',' + '$2');
} return x1 + x2;
} /**
* Usage: addCommas(12345678);
* result: 12,345,678
**/
/** 这个函数可以添加分隔逗号或者进行四舍五入。 **/

function number_format (number, decimals, dec_point, thousands_sep) {

    number = (number + '').replace(/[^-+-Ee.]/g, '');
var n = !isFinite(+number) ? : +number,
prec = !isFinite(+decimals) ? : Math.abs(decimals),
sep = (typeof thousands_sep === 'undefined') ? ',' : thousands_sep,
dec = (typeof dec_point === 'undefined') ? '.' : dec_point,
s = '',
toFixedFix = function (n, prec) {
var k = Math.pow(, prec);
return '' + Math.round(n * k) / k;
};
// Fix for IE parseFloat(0.55).toFixed(0) = 0;
s = (prec ? toFixedFix(n, prec) : '' + Math.round(n)).split('.');
if (s[].length > ) {
s[] = s[].replace(/B(?=(?:d{})+(?!d))/g, sep);
}
if ((s[] || '').length < prec) {
s[] = s[] || '';
s[] += new Array(prec - s[].length + ).join('');
}
return s.join(dec);
} /**
* Usage: number_format(123456.789, 2, '.', ',');
* result: 123,456.79
**/
/** 给数字添加"st, nd, rd, th"等序数词。 **/

Number.prototype.toOrdinal = function() {
var n = this % ;
var suffix = ['th', 'st', 'nd', 'rd', 'th'];
var ord = n < ? (n < ? suffix[n] : suffix[]) : (n % > ? suffix[] : suffix[n % ]);
return this + ord;
} /*
* Usage:
* var myNumOld = 23
* var myNumNew = myNumOld.toOrdinal()
* Result: 23rd
*/