From this post: How to deal with floating point number precision in JavaScript?
You have a few options:
- Use a special datatype for decimals, like decimal.js
- Format your result to some fixed number of significant digits, like this:
(Math.floor(y/x) * x).toFixed(2)
- Convert all your numbers to integers
From this post: How to deal with floating point number precision in JavaScript?
You have a few options:
- Use a special datatype for decimals, like decimal.js
- Format your result to some fixed number of significant digits, like this:
(Math.floor(y/x) * x).toFixed(2)
- Convert all your numbers to integers
You could do something like this:
> +(Math.floor(y/x)*x).toFixed(15);
1.2
Edit: It would be better to use big.js.
big.js
A small, fast, easy-to-use library for arbitrary-precision decimal arithmetic.
>> bigX = new Big(x)
>> bigY = new Big(y)
>> bigY.div(bigX).round().times(bigX).toNumber() // => 1.2
How can I deal with floating point number precision in JavaScript? - Stack Overflow
[AskJS] How do you deal with floats in production apps?
Floating point precision
[AskJS] Its about time javascript should get a "integer" and "float" data type.
Q4. How do I fix floating point rounding issues in JavaScript calculations?
Q7. Why are floating point errors in JavaScript so common?
Q2. How can I compare floating point numbers accurately in JavaScript?
From the Floating-Point Guide:
What can I do to avoid this problem?
That depends on what kind of calculations you’re doing.
- If you really need your results to add up exactly, especially when you work with money: use a special decimal datatype.
- If you just don’t want to see all those extra decimal places: simply format your result rounded to a fixed number of decimal places when displaying it.
- If you have no decimal datatype available, an alternative is to work with integers, e.g. do money calculations entirely in cents. But this is more work and has some drawbacks.
Note that the first point only applies if you really need specific precise decimal behaviour. Most people don't need that, they're just irritated that their programs don't work correctly with numbers like 1/10 without realizing that they wouldn't even blink at the same error if it occurred with 1/3.
If the first point really applies to you, use BigDecimal for JavaScript or DecimalJS, which actually solves the problem rather than providing an imperfect workaround.
I like Pedro Ladaria's solution and use something similar.
function strip(number) {
return (parseFloat(number).toPrecision(12));
}
Unlike Pedros solution this will round up 0.999...repeating and is accurate to plus/minus one on the least significant digit.
Note: When dealing with 32 or 64 bit floats, you should use toPrecision(7) and toPrecision(15) for best results. See this question for info as to why.
We all know the 0.1 + 0.2 = 0.30000000000000004 and the precision issues with Javascript floats (IEEE-754). These problems are immediately visible (and applicable) to nearly all application which has number/floats (even simple calculation via JS) on both frontend and backend with Node.js/Deno.js/Bun.js etc.
How do you deal with the fact that the floating point, which is the result of a calculation, is represented exactly and is saved correctly in DB/REST api/front end etc.