1.功能:将浮点数四舍五入,取小数点后2位
function toDecimal(num) {
var f = parseFloat(num);
if (isNaN(f)) {
return;
}
f = Math.round(num*100)/100;
return f;
}
document.write(toDecimal(2.2291));
结果:2.23
2.功能:取小数点后2位,不四舍五入
function get(num)
{
var str = num.substring(0,num.indexOf(".") + 3);
return str;
}
document.write(toDecimal(2.2291));
结果:2.22
3.功能:取小数点后2位,不四舍五入
function getnum(num){
var re = /([0-9]+\.[0-9]{2})[0-9]*/;
aNew = num.replace(re,"$1");
return aNew;
}
document.write(getnum("23.456322"));
结果:23.45
4.功能:将浮点数四舍五入,取小数点后2位 (需要 IE5.5+才支持)
var num=22.127456;
document.write(num.toFixed(2)));
结果:22.13
5.功能:将浮点数四舍五入,取小数点后2位
function decimal(num,v)
{
var vv = Math.pow(10,v);
return Math.round(num*vv)/vv;
}
document.write(decimal("2.129",2));
结果:2.13