1.通过substring截取。
1 2 3 4 5 6 | function getnum()
{
var num = 22.123456;
var result = num.substring(0,s.indexOf( "." )+3);
alert(result);
}
|
Copy after login
2. 正则表达式。
1 2 3 4 5 6 7 8 | function getnum()
{
var num = 22.123456;
var aNew;
var re = /([0-9]+\.[0-9]{2})[0-9]*/;
aNew = num.replace(re, "$1" );
alert(aNew);
}
|
Copy after login
3.数据类型保留上。
1 2 3 4 5 | function getnum()
{
var num=22.123456;
alert( Math. round (num*100)/100);
}
|
Copy after login
4.toFixed方法
The above is the detailed content of Several examples of methods to get the number of decimal places javascript. For more information, please follow other related articles on the PHP Chinese website!