用js語言將錢四舍五入到最接近的0.05尋求幫助。
輸入和預期輸出:
1.10 => 1.10
1.11 => 1.10 (round down)
1.12 => 1.10 (round down)
1.13 => 1.15 (round up)
1.14 => 1.15 (round up)
1.15 => 1.15
1.16 => 1.15 (round down)
1.17 => 1.15 (round down)
1.18 => 1.20 (round up)
1.19 => 1.20 (round up)

來自:https ://www.bnm.gov.my/misc/-/asset_publisher/2BOPbOBfILtL/content/frequently-asked-questions-faqs-on-rounding-mechanism
uj5u.com熱心網友回復:
您可以采用偏移量并采用多個底值。
如果最后需要零,請使用.toFixed(2).
const format = f => Math.floor((f 0.025) * 20) / 20;
console.log([1.10, 1.11, 1.12, 1.13, 1.14, 1.15, 1.16, 1.17, 1.18, 1.19].map(format));
.as-console-wrapper { max-height: 100% !important; top: 0; }
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/449142.html
標籤:javascript 数学 四舍五入
上一篇:如何在顫振中實作ln(x)
