转载自:http://blog.csdn.net/wamlko/article/details/24641187

Javascript取float型小数点后两位,例22.123456取成22.12,如何做?

1.通过substring截取。

1     function getnum()  
2     {  
3     var num = 22.123456;  
4     var result = num.substring(0,s.indexOf(".")+3);  
5     alert(result);  
6     }  

2. 正则表达式。

 

1     function getnum()  
2     {  
3     var num = 22.123456;  
4     var aNew;  
5     var re = /([0-9]+\.[0-9]{2})[0-9]*/;  
6     aNew = num.replace(re,"$1");  
7     alert(aNew);  
8     }  

 

3.数据类型保留上。

 

1     function getnum()  
2     {  
3     var num=22.123456;  
4     alert( Math.round(num*100)/100);  
5     }  

 

4.toFixed方法

 alert(num.toFixed(2));