Description
So I wrote the following calculation:
var valueWithoutIntegers = value - Math.floor(value);
This statement seems to do the job just fine, except when I had the value value = 300.2
. So basically I had :
0.19999999999998863 = 300.2 - Math.floor(300.2)
So I fiddle around quickly in my JavaScript console:
300.2 - Math.floor(300.0) // gives 0.19999999999998863
300.2 - Math.floor(300.1) // gives 0.19999999999998863
300.2 - Math.floor(300.0) // gives 0.19999999999998863
300.2 - 300 // gives 0.19999999999998863
300.2 - 300.1 // gives 0.0999999999999659
So I was all like... WTF right? But I thought, maybe I need to help the laws of physics, I mean floating points in computer science. So I googled a bit and tried out:
Number((300.2).toFixed(2)) - Number((300.2).toFixed(0)) // gives 0.19999999999998863
Number((300.2).toFixed(2)) - Number((300.2).toFixed(2)) // gives 0, makes sense!
300.2 - Number((300.2).toFixed(0)) // gives 0.19999999999998863
Number(300.2) - Number(300) // gives 0.19999999999998863
Yikes ! Feel free to run any of these statements in your JavaScript console. I ran it in my Google Chrome and in my local Node.JS application.
So my questions
- Where does this magic come from? I don't see why this would be a corner case, I only used 1 number after the decimal! Not like, 200.
- How can I refactor my statement, so I won't have to deal with this or other edge cases?