2010-06-04 93 views

回答

4

嘗試使用toFixed:

alert(i.toFixed(2)); 

如果您需要從Jappie下一個答案中提到的精度,可以覆蓋這樣的本地toFixed方法:

Number.prototype.toFixed = function (precision) { 
var power = Math.pow(10, precision || 0); 
return String(Math.round(this * power)/power); 
}; 
2

如何

alert(Math.round(i * 100)/100); 

沒有與toFixed問題。見this post

0

100%的工作!!!!

<html> 
 
    <head> 
 
     <script> 
 
     function replacePonto(){ 
 
     var input = document.getElementById('qtd'); 
 
     var ponto = input.value.split('.').length; 
 
     var slash = input.value.split('-').length; 
 
     if (ponto > 2) 
 
       input.value=input.value.substr(0,(input.value.length)-1); 
 

 
     if(slash > 2) 
 
       input.value=input.value.substr(0,(input.value.length)-1); 
 

 
     input.value=input.value.replace(/[^0-9.-]/,''); 
 

 
     if (ponto ==2) 
 
\t input.value=input.value.substr(0,(input.value.indexOf('.')+3)); 
 

 
if(input.value == '.') 
 
\t input.value = ""; 
 
       } 
 
     </script> 
 
     </head> 
 
     <body> 
 
     <input type="text" id="qtd" maxlength="10" style="width:140px" onkeyup="return replacePonto()"> 
 
     </body> 
 
    </html>