JQuery
/**
* 将数值四舍五入(保留2/4位小数)后格式化成金额形式
*
* @param num 数值(Number或者String)
* @return 金额格式的字符串
* @type String
*/
function formatCurrency(num, no) {
if(num != 0) {
num = num.toString().replace(/\$|\,/g,'');
if(isNaN(num))
num = "0";
sign = (num == (num = Math.abs(num)));
if(no === 2){
num = Math.floor(num*100);
cents = num%100;
num = Math.floor(num/100).toString();
}
if(no === 4){
num = Math.floor(num*10000);
cents = num%10000;
num = Math.floor(num/10000).toString();
}
if(cents<10 && no == 2) {
cents = "0" + cents;
}
if(cents<10 && no == 4) {
cents = "000" + cents;
}
for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
num = num.substring(0,num.length-(4*i+3))+','+
num.substring(num.length-(4*i+3));
return (((sign)?'':'-') + num + '.' + cents);
} else {
return " ";
}
}
/**
* 将数值四舍五入(保留2位小数)后格式化成金额形式
*
* @param num 数值(Number或者String)
* @return 金额格式的字符串,如'1,234,567.45'
* @type String
*/
function formatCurrency(num) {
num = num.toString().replace(/\$|\,/g,'');
if(isNaN(num))
num = "0";
sign = (num == (num = Math.abs(num)));
num = Math.floor(num*100+0.50000000001);
cents = num%100;
num = Math.floor(num/100).toString();
if(cents<10)
cents = "0" + cents;
for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
num = num.substring(0,num.length-(4*i+3))+','+
num.substring(num.length-(4*i+3));
return (((sign)?'':'-') + num + '.' + cents);
}
/**
* 将数值四舍五入(保留1位小数)后格式化成金额形式
*
* @param num 数值(Number或者String)
* @return 金额格式的字符串,如'1,234,567.4'
* @type String
*/
function formatCurrencyTenThou(num) {
num = num.toString().replace(/\$|\,/g,'');
if(isNaN(num))
num = "0";
sign = (num == (num = Math.abs(num)));
num = Math.floor(num*10+0.50000000001);
cents = num%10;
num = Math.floor(num/10).toString();
for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
num = num.substring(0,num.length-(4*i+3))+','+
num.substring(num.length-(4*i+3));
return (((sign)?'':'-') + num + '.' + cents);
}
// 添加金额格式化
jQuery.extend({
formatFloat:function(src, pos){
var num = parseFloat(src).toFixed(pos);
num = num.toString().replace(/\$|\,/g,'');
if(isNaN(num)) num = "0";
sign = (num == (num = Math.abs(num)));
num = Math.floor(num*100+0.50000000001);
cents = num%100;
num = Math.floor(num/100).toString();
if(cents<10) cents = "0" + cents;
for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
num = num.substring(0,num.length-(4*i+3))+','+num.substring(num.length-(4*i+3));
return (((sign)?'':'-') + num + '.' + cents);
}
});
Java
- 方法一
NumberFormat numberFormat = NumberFormat.getInstance();// //返回当前缺省语言环境的缺省数值格式。
numberFormat.setMinimumFractionDigits(2);//设置数值的小数部分允许的最小位数。
numberFormat.setMaximumFractionDigits(2);//设置数值的小数部分允许的最大位数。
numberFormat.setMaximumIntegerDigits(10);//设置数值的整数部分允许的最大位数.
numberFormat.setMinimumIntegerDigits(0);//设置数值的整数部分允许的最小位数.
System.out.println(numberFormat.format(1002200999.22323)); //1,002,200,999.22
- 方法二
DecimalFormat decimalFormat = new DecimalFormat("###,###.00");
System.out.println(decimalFormat.format(1002200999.22323)); //1,002,200,999.22
DecimalFormat
是NumberFormat
的子类,两种方式实现的效果是一样的。
- 其他
NumberFormat getNumberInstance();//获取当前系统的通用数值格式
NumberFormat getIntegerInstance();//获取当前系统的通用整型格式
NumberFormat.getCurrencyInstance();//获取当前系统通用金额格式
NumberFormat.getPercentInstance();//获取当前系统通用百分比格式
- DecimalFormat 中的 # 与 0 的区别
String format = new DecimalFormat("###,###.00").format(0f);//结果为.00
String format2 = new DecimalFormat("###,##0.00").format(0f);//结果为0.00