0.1 + 0.2 === 0.3 在 JavaScript 裡會回傳 false,這是浮點數表示的先天限制。若要做金額或科學計算,就得自己處理精度。下面的做法是:把運算數視為整數來算,最後再推回小數點。
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
</head>
<script language="javascript" type="text/javascript">
// 除法
function accDiv(arg1, arg2) {
var t1 = 0, t2 = 0, r1, r2, n;
try { t1 = arg1.toString().split(".")[1].length; } catch (e) { t1 = 0; }
try { t2 = arg2.toString().split(".")[1].length; } catch (e) { t2 = 0; }
with (Math) {
r1 = Number(arg1.toString().replace(".", ""));
r2 = Number(arg2.toString().replace(".", ""));
n = Math.max(t1, t2);
return (r1 / r2) * pow(10, t2 - t1);
}
}
// 乘法
function accMul(arg1, arg2) {
var t1 = 0, t2 = 0, r1, r2;
try { t1 = arg1.toString().split(".")[1].length; } catch (e) { t1 = 0; }
try { t2 = arg2.toString().split(".")[1].length; } catch (e) { t2 = 0; }
with (Math) {
r1 = Number(arg1.toString().replace(".", ""));
r2 = Number(arg2.toString().replace(".", ""));
return (r1 * r2) / pow(10, t2 + t1);
}
}
// 加法
function accAdd(arg1, arg2) {
var t1 = 0, t2 = 0, m;
try { t1 = arg1.toString().split(".")[1].length; } catch (e) { t1 = 0; }
try { t2 = arg2.toString().split(".")[1].length; } catch (e) { t2 = 0; }
with (Math) {
m = Math.pow(10, Math.max(t1, t2));
return (arg1 * m + arg2 * m) / m;
}
}
// 減法
function accSubtr(arg1, arg2) {
var t1 = 0, t2 = 0, m, n;
try { t1 = arg1.toString().split(".")[1].length; } catch (e) { t1 = 0; }
try { t2 = arg2.toString().split(".")[1].length; } catch (e) { t2 = 0; }
with (Math) {
n = Math.max(t1, t2); // 依精度決定小數位數
m = Math.pow(10, n);
return ((arg1 * m - arg2 * m) / m).toFixed(n);
}
}
// 擴充 String,讓呼叫更直覺
String.prototype.div = function (arg) { return accDiv(this, arg); };
String.prototype.mul = function (arg) { return accMul(arg, this); };
String.prototype.add = function (arg) { return accAdd(arg, this); };
String.prototype.subtr = function (arg) { return accSubtr(this, arg); };
function cal() {
var arg1 = document.Form1.TextBox1.value;
var arg2 = document.Form1.TextBox2.value;
document.Form1.TextBox5.value = arg1.div(arg2);
document.Form1.TextBox6.value = arg1.mul(arg2);
document.Form1.TextBox7.value = arg1.add(arg2);
document.Form1.TextBox8.value = arg1.subtr(arg2);
}
</script>
<body>
<form id="Form1" name="Form1" method="post" runat="server">
<div style="border:solid 1px #000000; width:600px;">
<div style="float:left; width:30%;">
<input id="TextBox1" type="text" value="0" name="TextBox1" />
</div>
<div style="float:left; width:30%;">
<input id="TextBox2" value="0" type="text" name="TextBox2" />
</div>
<div style="float:left; width:30%;">
<div>accDiv:<input id="TextBox5" type="text" name="TextBox5" /></div>
<div>accMul:<input id="TextBox6" type="text" name="TextBox6" /></div>
<div>accAdd:<input id="TextBox7" type="text" name="TextBox7" /></div>
<div>accSubtr:<input id="TextBox8" type="text" name="TextBox8" /></div>
</div>
<div style="float:right; width:10%;">
<input type="button" name="aa" value="cal" onclick="cal();" />
</div>
</div>
</form>
</body>
</html>
with 陳述式不建議使用:它在嚴格模式下被禁用,也容易讓程式碼意圖變模糊。改寫時把 Math 的呼叫寫全(Math.pow)即可,功能完全相同。
擴充內建原型要謹慎:直接改 String.prototype 在多人協作或引入第三方函式庫時可能撞名。比較安全的做法是改用獨立命名空間或 ES Module 匯出。
更現代的替代方案:若只是為了通貨計算,直接用「以最小貨幣單位為整數」來運算通常最省事;需要通用十進位精度時,可考慮 decimal.js、big.js 這類專門的十進位函式庫。