【发布时间】:2013-03-26 16:29:50
【问题描述】:
我正在用Javascript做一个简单的减法,结果与实际结果不匹配:
<html>
<head>
<title></title>
</head>
<body>
<table>
<tr><td>Price: </td><td><input type="text" id="price" value="4500.60" onblur="calculatePrecio();"></td></tr>
<tr><td>Discount: </td><td><input type="text" id="discount" value="500" onblur="calculatePrecio();"></td></tr>
</table>
<div id="divTotalAmount"></div>
<script>
calculatePrecio();
function calculatePrecio()
{
var price = document.getElementById('price').value;
var discount = document.getElementById('discount').value;
p = price;
d = discount;
totalAmount = p - d;
document.getElementById('divTotalAmount').innerHTML = "<h1>"+totalAmount+"</h1>";
}
</script>
</body>
</html>
你们能帮帮我吗?另外我只是不想知道如何进行,如果可能的话,我想知道原因。
【问题讨论】:
-
阅读浮点运算:floating-point-gui.de
-
您可以使用
.toFixed(),正如答案所暗示的那样,但使用浮点数字系统进行“金钱数学”总是有些危险。 -
欢迎堆栈溢出。 :)
标签: javascript decimal arithmetic-expressions