Answer the question
In order to leave comments, you need to log in
Why does toFixed round up like that?
0.755.toFixed(2); // result 0.76
0.7555.toFixed(3); // result 0.755
0.75555.toFixed(4); // result 0.7556
0.755555.toFixed(5); // result 0.75555
0.7555555.toFixed(6); // result 0.755556
0.75555555.toFixed(7); // result 0.7555555
Why is this happening, because in these examples there should always be a 6 at the end.?
Answer the question
In order to leave comments, you need to log in
https://learn.javascript.ru/number#%D0%BD%D0%B5%D1...
Because these numbers cannot be represented exactly in double format (see IEEE 754)
> 0.755.toFixed(20)
'0.75500000000000000444'
> 0.7555.toFixed(20)
'0.75549999999999994937'
> 0.75555.toFixed(20)
'0.75555000000000005489'
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question