PDA

View Full Version : how to check if a var is a decimal?



mindfriction
February 28th, 2003, 01:21 AM
Hi,

I need to check whether a varaible is an odd number, as I alternate which part of the movie I play.

something like....

if(i=oddNumb)
{
gotoAndPlay(...)
}
else
{
gotoAndPlay(...)
}


I was thinking if i could take the value of the variable divide it by 2, and then check whether the answer is a decimal and then I could tell if it was an odd number., But Im not sure how to do this even.

Your help appreciated

mindfriction
February 28th, 2003, 02:08 AM
Oh well after a little while Iactually found a way: :D



theNumb=3;

ans = theNumb/2;
trace(ans);
check = Math.round(ans);
trace(check);
if(check == ans)
{
trace("is EVEN");

}
else
{
trace("is ODD");
}

pom
February 28th, 2003, 08:54 AM
There's an easier and shorter way to determine whether a variable is odd or even with the modulo operator:

if a number is even, it's possible to divide it by 2 (the rest equals 0) so you could create a little prototype:
Number.prototype.isEven=function(){
return !(this%2) ;
}this%2 returns 0 is the number is even and 1 if it's odd, so we take the opposite with !.

Cheers.

mindfriction
February 28th, 2003, 12:18 PM
Thanks, :)

Its actually for the same flash which Im trying to use prototypes in (my other recent posting) , but alas Ive almost given up on the prototyping as I dont seem to completely understand it :/

(other posting re prototypes)
http://www.kirupaforum.com/showthread.php?s=&threadid=6636