Why does this happen when I print / display the result
eval("11.05") + eval("-11")
it is output as 0.05000000000000071 instead of the expected 0.05. Is something missing?
This has nothing to do with eval. In fact, this is what happens if you type the console 11.05 - 11:
eval
11.05 - 11
This is a consequence of how programming languages store floating point numbers; they include a small mistake. If you want to know more about this, check it out .
This has nothing to do with eval(which you should avoid).
You get the same problem with 11.05 - 11.
, eval. eval: , :
Number("11.05") + Number("-11");
, toPrecision:
toPrecision
(Number("11.05") + Number("-11")).toPrecision(12); // or if you want 0.05 to be the outcome (Number("11.05") + Number("-11")).toPrecision(1);
eval . - . , . .