Hi im trying to subtract 2 decimal numbers and it keeps returning some weird number.
var x = 0.00085022
var y = 0.00085050
var answer = x - y
alert(answer)
This is the number its returning -2.8000000000007186e-7
Hi im trying to subtract 2 decimal numbers and it keeps returning some weird number.
var x = 0.00085022
var y = 0.00085050
var answer = x - y
alert(answer)
This is the number its returning -2.8000000000007186e-7
The maximum number of decimals is 17, but floating point arithmetic is not always 100% accurate http://www.w3schools.com/js/js_numbers.asp
Try this:
var x = 0.00085022 * 100000000;
var y = 0.00085050 * 100000000;
var answer = (x - y) / 100000000;
alert(answer);
You are subtracting with a higher number and the calculations are traversing to an even lower number. Yes -2.0 is lower and the decimal places precision is reaching exponentially higher.
If we round them up we get:
var x = 85022
var y = 85050
var answer = x - y
alert(answer); // = -28