8 Replies Latest reply on Jul 17, 2006 5:34 PM by Newsgroup_User

# Finding the remainder of a division

Hi.

I want to do a division in actionscript, say 49/2 and I want to return 2 variables - the integer that goes into 49 twice=24 and the remainder- 1. I also want this to return a remainder of 0 if there is no remainder.

I'm just a little stuck on the code i'd use.

Mac
try:

• ###### 2. Re: Finding the remainder of a division
Thanks for that quick reply. What I need to do now, is loop through this somehow. Basically I want to loop these statements and record the values in seperate variables each time, until I am left with div=0

So basically 49/2= 24 r 1
24/2=12 r 0
12/2=6 r 0
6/2= 3 r0
3/2= 1 r 1
1/2=0 r 1

It's mainly the loop bit that I'm stuck on, as ill need to refer to the integers individually later.
div1=24, div 2=12 div3=6 etc.
• ###### 3. Re: Finding the remainder of a division
are you trying to calculate the binary representation of a number (like 49)? if so, that's not quite right. if not, you can use the following function to store the values you want in an array a:

• ###### 4. Re: Finding the remainder of a division
Yes I actually I was trying to convert a number to binary using that method..the one found here:

http://www.janeg.ca/scjp/oper/binhex.html

However, I've since realised for what I'm doing hex would be more suitable. I was going to use 49/16 instead of 49/2..but you say that method is wrong? any help greatly appreciated.

Ta,
mac
• ###### 5. Re: Finding the remainder of a division

var n:Number = 49;
trace(n.toString(2));
trace(n.toString(8));
trace(n.toString(16));

"mac monkey" <webforumsuser@macromedia.com> wrote in message
news:e9ea08\$ovc\$1@forums.macromedia.com...
> Yes I actually I was trying to convert a number to binary using that
> method..the one found here:
>
> http://www.janeg.ca/scjp/oper/binhex.html
>
> However, I've since realised for what I'm doing hex would be more
> suitable. I
> was going to use 49/16 instead of 49/2..but you say that method is wrong?
> any
> help greatly appreciated.
>
> Ta,
> mac
>

• ###### 6. Re: Finding the remainder of a division
Awesome, cheers. That saves me alot of work lol
• ###### 7. Re: Finding the remainder of a division
That works excellently when I used var n:Number = 49;

but when I use var n:Number = dec;
etc.
inside an on release instance where dec is the variable for an input field my traces return 3 of 49.
so: 49,49,49 rather than:110001,61,31
No doubt its a scoping problem, I just can't figure out where.
• ###### 8. Re: Finding the remainder of a division
dec is a string

Cast it to a Number first:

Number(dec).toString(2);
Number(dec).toString(8);
Number(dec).toString(16);

Try casting it to a Number first.
"mac monkey" <webforumsuser@macromedia.com> wrote in message
news:e9gulv\$4mp\$1@forums.macromedia.com...
> That works excellently when I used var n:Number = 49;
>
> but when I use var n:Number = dec;
> etc.
> inside an on release instance where dec is the variable for an input field
> my
> traces return 3 of 49.
> so: 49,49,49 rather than:110001,61,31
> No doubt its a scoping problem, I just can't figure out where.
>