Strange floating-point result

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?

+3
source share
4 answers

This has nothing to do with eval. In fact, this is what happens if you type the console 11.05 - 11: enter image description here

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 .

+5
source

This has nothing to do with eval(which you should avoid).

You get the same problem with 11.05 - 11.

+3

, eval. eval: , :

Number("11.05") + Number("-11");

, 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);
+1

eval . - . , . .

0

All Articles