js取小数点后两位四种方法
// 1.通过substring截取。function getnum() {var num = 22.123456;var s = num.toString();var result = s.substring(0,s.indexOf(".")+3);var result2 = s.substring(0,s.indexOf(".")+5);console.log(result);console.
·
// 1.通过substring截取。
function getnum() {
var num = 22.123456;
var s = num.toString();
var result = s.substring(0,s.indexOf(".")+3);
var result2 = s.substring(0,s.indexOf(".")+5);
console.log(result);
console.log(result2);
}
getnum();
// 2. 正则表达式。
function getnum() {
var num = 22.123456;
var aNew;
var re = /([0-9]+\.[0-9]{4})[0-9]*/;
aNew = num.toString().replace(re,"$1");
alert(aNew);
}
getnum();
// 3.数据类型保留上。
function getnum() {
var num=22.123456;
console.log( Math.round(num*10000)/10000);
console.log( Math.round(num*1000)/1000);
console.log( Math.round(num*100)/100);
console.log( Math.round(num*10)/10);
}
getnum();
// 4.toFixed方法
let num = 22.123456;
alert(num.toFixed(4));
更多推荐
已为社区贡献7条内容
所有评论(0)