- 去位取整
// 去位取小数
function decimalPlaces(num, decimal) {
var pow = Math.pow(10, decimal);
var res= parseInt(num*pow)/pow;
return res;
}
例:
decimalPlaces(1.5888888, 2)
返回 1.58
- 四舍五入
num.toFixed();
例:
num = 1.58888;
num.toFixed(2);
返回 1.59