【发布时间】:2016-01-25 10:47:03
【问题描述】:
我正在尝试将此脚本的结果给出小数点后 2 位。它们实际上都已经有 2 位小数,但是当我将最后 2 位加在一起 (35.75 + 16.06) 时,我得到 €51.629999999999995 作为输出。
这是我的脚本;
<!DOCTYPE html>
<html>
<head>
<script>
function bereken() {
var total = 0;
if (document.forms[0].boek1.checked) {
total += 27.74;
}
if (document.forms[0].boek2.checked) {
total += 26.13;
}
if (document.forms[0].boek3.checked) {
total += 35.57;
}
if (document.forms[0].boek4.checked) {
total += 16.06;
}
totalP = "€" + total
document.forms[0].total.value = totalP;
}
</script>
</head>
<body>
<form>
<input type="checkbox" name="boek1">Boek 1<br>
<input type="checkbox" name="boek2">Boek 2<br>
<input type="checkbox" name="boek3">Boek 3<br>
<input type="checkbox" name="boek4">Boek 4<br>
<div><input type="button" value="Totaal" onclick="bereken()" /><br><br>
<input type="text" value="€" name="total" size="5" />
</form>
</body>
你们有谁知道我做错了什么吗?
【问题讨论】:
-
这是一个错误的 HTML BTW:
<! DOCTYPE html>
标签: javascript numbers decimal