Question

I'm having a problem with adding numbers in javascript. I have a variable that keeps track of full number (currentfloatx) and a variable that keeps track of the floored version of that number (newintx). I'm trying to add .25 to currentfloatx, but for some reason its multiplying the number by 10. Does anyone know why it's doing this? Is there something about how javascript is handling these number that I'm missing? Thanks.

//currentfloatx is currently set to 6
alert(currentfloatx + .25);   //returns 60.25
alert(currentfloatx);         //returns 6
newintx = Math.floor(currentfloatx + .25);
alert(newintx);           //returns 60.25
Was it helpful?

Solution

The only way I can think of that would make this happen is if currfloatx holds a string (eg., "6"). This would make currfloatx + .25 be the equivalent of "6" + "0.25", or "60.25".

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top