Precision Loss in JavaScript Calculations
This post was translated from Chinese by AI. If anything reads oddly, the Chinese original is authoritative. 中文原文
Example
Ï
In the example above, when the unit price is 35.3 and the quantity is 3, the calculated result is 105.899999, not 105.9.
I asked chatgpt, and this was the answer:
This happens because floating-point calculations in JavaScript can lose precision. Floating-point numbers are represented internally in binary, but some decimal fractions cannot be converted exactly to binary, which introduces small errors during calculations.
To address this, you can use the toFixed method provided by JS to keep a specified number of decimal places, or convert floating-point values to integers for calculation and then adjust the result accordingly.
If you need a fixed number of decimal places, you can use toFixed. If you don't need a fixed number of decimal places or need higher precision, you can convert the values to integers for calculation. For currency calculations, a dedicated library (such as Big.js) is recommended to avoid precision loss.
So
The relevant part of the code above should be changed to:
const amount = item.invoice_pcs * item.invoice_unitprice;
item.invoice_amount = amount.toFixed(3);

That fixes the issue.
Last updated 2024-01-14
Comments 0