Omnimaga

General Discussion => Technology and Development => Web Programming and Design => Topic started by: Sorunome on January 21, 2012, 05:42:21 am

Title: Javascript integerpart
Post 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!
Title: Re: Javascript integerpart
Post by: ZippyDee on January 21, 2012, 05:53:19 am
Math.floor(number)
Title: Re: Javascript integerpart
Post by: Sorunome on January 21, 2012, 06:08:38 am
Thanks, that's working! :)
Title: Re: Javascript integerpart
Post by: ZippyDee on January 21, 2012, 06:14:43 am
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.
Title: Re: Javascript integerpart
Post by: Sorunome on January 21, 2012, 06:16:13 am
Well, as I just use it with positive numbers that doesn't matter.
Title: Re: Javascript integerpart
Post by: ZippyDee on January 21, 2012, 06:23:02 am
Okay, I just wanted to make sure. If you only use it with positive then Math.floor will do the trick for sure.