I have a large amount of numeric values y
in javascript. I want to group them by rounding them down to the nearest multiple of x
and convert the result to a string.
How do I get around the annoying floating point precision?
For example:
0.2 + 0.4 = 0.6000000000000001
Two things I have tried:
>>> y = 1.23456789 >>> x = 0.2 >>> parseInt(Math.round(Math.floor(y/x))) * x; 1.2000000000000002
and:
>>> y = 1.23456789 >>> x = 0.2 >>> y - (y % x) 1.2000000000000002
double has 2x more precision than float. float is a 32-bit IEEE 754 single precision Floating Point Number – 1 bit for the sign, 8 bits for the exponent, and 23* for the value. float has 7 decimal digits of precision.
The representation of floating points in JavaScript follows the IEEE-754 format. It is a double precision format where 64 bits are allocated for every floating point.
A variable of type float only has 7 digits of precision whereas a variable of type double has 15 digits of precision. If you need better accuracy, use double instead of float.
From this post: How to deal with floating point number precision in JavaScript?
You have a few options:
(Math.floor(y/x) * x).toFixed(2)
You could do something like this:
> +(Math.floor(y/x)*x).toFixed(15); 1.2
Edit: It would be better to use 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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With