2011-06-29 4 views
11

にトリム:私は必要なものである2進ポイントにラウンドOK私が持っている2つの小数

onclick="document.getElementById('field1').value = 
Math.round((parseFloat(document.getElementById('field2').value,2)*100))/100 + 
Math.round((parseFloat(document.getElementById('field3').value,2)*100))/100;" 

ほとんどの数字を。

しかし、

onclick="document.getElementById('field1').value = 
Math.round((parseFloat(document.getElementById('21.29').value,2)*100))/100 + 
Math.round((parseFloat(document.getElementById('54.70').value,2)*100))/100;" 

フィールドのような例を図1は、私が一貫し75.99にトリミングすることができどのよう75.99000000000001を返していますか?

答えて

5

は、小数点以下2桁でそれを修正する方法toFixed(2)を使用します。

(Math.round((parseFloat(document.getElementById('21.29').value,2)*100))/100 + 
    Math.round((parseFloat(document.getElementById('54.70').value,2)*100))/100).toFixed(2); 
2

これについてどのように:

parseFloat(document.getElementById('21.29').toFixed(2);

toFixedメソッドがあなたのためにうまく丸めの世話をする必要があります。

31
var num = 5/6; 

var display = num.toFixed(2) 

num outputs: 0.8333333333333334 

display outputs: "0.83" 
関連する問題