javascript 保留小数点后几位几种方法
1.通过substring截取
var num = 11.456789;
var result = num.substring(0,s.indexOf(".")+3);
console.log(result);
2.通过正则表达式
var num = 11.456789;
var result ;
var reg= /([0-9]+\.[0-9]{3})[0-9]*/;
result = num.replace(reg,"$1");
console.log(result);
3.数据类型保留(只保留了两位)
var num = 11.456789;
var result = Math.round(num*100)/100;
console.log(result);
4.通过toFixed方法
var num = 11.456789;
var result = num.toFixed(2)
console.log(result);