Why does toFixed()
not work as expected for the following case? What is the solution?
Code:
var num = 5.56789;
var n = num.toFixed(16);
console.log(num)
console.log(n)
Expected value of n: 5.5678900000000000
Actual value of n: 5.5678900000000002
Note:
- I have gone through all the relevant stack overflow questions.
- I understand it's not giving the expected results because floating-point numbers cannot be represented perfectly in binary.
- What I am expecting is a library function that actually acknowledges the above and generates the expected output.