+ 1

Weird floats in JS?

Hello, coders! I've got possibly very stupid question about JavaScript that most certainly have an answer somewhere out there. Here goes... Suppose, I have two vars: i = 6 and di = 0.2 Every time a button is pressed runs following bit of code: i = i + di; As a result I get series of numbers: 6, 6.2, 6.4, 6.6000000000000005, 6.800000000000001, etc... Why is this happening? Can I somehow limit my floats to one decimal? Or do I have to round var 'i' each iteration? I know, it would probably be easier to give up floats altogether in this particular case (by multiplying by 10), but I'd like to understand what's going on at least.

6th May 2017, 5:28 PM
Billie ToRt
6 Antworten
+ 8
found this explanation https://docs.python.org/release/2.5.1/tut/node16.html
6th May 2017, 5:56 PM
Burey
Burey - avatar
+ 5
💪
6th May 2017, 6:52 PM
Burey
Burey - avatar
+ 2
deals with how computers inaccurately represent floating point numbers. its not your fault. you can google more on this subject
6th May 2017, 5:49 PM
Edward
+ 2
Oh! Thanks a lot! =) It was very confusing for a moment there, but I see it now. It's just that I never quite had this problem before in C++, although to be fair it probably has to do with the fact that there you can easily specify how many decimals you want to print. So, I suppose I'd have to find a little workaround to achieve similar effect.
6th May 2017, 6:08 PM
Billie ToRt
+ 2
Much appreciated, Burey =) I'll get right on it.
6th May 2017, 6:39 PM
Billie ToRt