DEV Community

Discussion on: JS float rounding bug

Collapse
 
pengeszikra profile image
Peter Vivo

You right js floating error is quite disturbing.
Here is a little bit more mathematical solution:

const epsilonN = N => num => Math.round( num * N + Number.EPSILON ) / N;
const epsilon2 = epsilonN(1e2);

0.1 + 0.2  // 0.30000000000000004
epsilon2(0.1 + 0.2) // 0.3
epsilon2(33.8 * 100) // 3380
Enter fullscreen mode Exit fullscreen mode
Collapse
 
bloodrave_n profile image
Pike Msonda

This looks much better and safe.