Omnimaga
General Discussion => Technology and Development => Web Programming and Design => Topic started by: Sorunome on January 21, 2012, 05:42:21 am
-
How do you get the integerpart of a variable?
so that 1.9 would also be 1
thanks for help!
-
Math.floor(number)
-
Thanks, that's working! :)
-
I believe that works like BASIC's int() though, not like iPart(), meaning that it rounds everything down. So -1.9 is -2 instead of -1...
If you want to have it round then just write a function like
function ipart(x){
if(x<0)return Math.ceil(x);
return Math.floor(x);
}
and use ipart(value) to get the integer part.
-
Well, as I just use it with positive numbers that doesn't matter.
-
Okay, I just wanted to make sure. If you only use it with positive then Math.floor will do the trick for sure.