# Rounding numbers - Macromedia Director Lingo

Hello, I have a variable (x) which is the number of items in a list (myList). I need to make some equation where if the number of items in myList is not divisible EXACTLY by 5, I need to divide that number by 5 and then round up to the nearest number which IS divisible by 5. So what I need is # of List items rounded value for x 1 5 2 5 3 5 4 5 5 5 6 10 7 10 8 10 9 10 10 10 11 15 etc... So every time the number of items ...

1. ## Rounding numbers

Hello, I have a variable (x) which is the number of items in a list (myList).

I need to make some equation where if the number of items in myList is not divisible EXACTLY by 5, I need to divide that number by 5 and then round up to the nearest number which IS divisible by 5.

So what I need is

# of List items rounded value for x
1 5
2 5
3 5
4 5
5 5
6 10
7 10
8 10
9 10
10 10
11 15

etc...

So every time the number of items in a list passes a multiple of 5, i need X to increase by 5 as well.
anyone have a good idea for this?
thanks
-Alexis

Ali Guest 2. ## Re: Rounding numbers

property myList

on randomList - Generate random number list
myList = []
repeat with i = 1 to random(100)
end repeat
end

on mouseUp
randomList() -- Just to test out the handler

divideNum = 5
lastItem = myList.count
a = myList.count mod divideNum
x = myList.count / divideNum

if a <> 0 then
x = x + 1
end if

x = x * divideNum

put lastItem,x

end

-- I wonder what happens If the list is empty...???? hmmm... you tell me. :)
-- To test this I place this script inside a sprite as a behavior. You´ll need to adept it to your needs.

=°o°= Arturo =°o°=

"Ali Mac" <com> escribió en el mensaje news:bu1lg7\$4fe\$macromedia.com...
Arturo Guest 3. ## Re: Rounding numbers UPDATED...

on mouseUp
randomList() -- Just to test out the handler
divideNum = 5

lastItem = myList.count

x = lastItem / divideNum

if lastItem mod divideNum > 0 then
x = x + 1
end if

x = x * divideNum

put lastItem,x

end

"Arturo Toledo" <tv> escribió en el mensaje news:bu1pmd\$9kn\$macromedia.com...
property myList

on randomList - Generate random number list
myList = []
repeat with i = 1 to random(100)
end repeat
end

on mouseUp
randomList() -- Just to test out the handler

divideNum = 5
lastItem = myList.count
a = myList.count mod divideNum
x = myList.count / divideNum

if a <> 0 then
x = x + 1
end if

x = x * divideNum

put lastItem,x

end

-- I wonder what happens If the list is empty...???? hmmm... you tell me. :)
-- To test this I place this script inside a sprite as a behavior. You´ll need to adept it to your needs.

=°o°= Arturo =°o°=

"Ali Mac" <com> escribió en el mensaje news:bu1lg7\$4fe\$macromedia.com...
Arturo Guest 4. ## Re: Rounding numbers

not sure I understand what you wrote.
Seemed more complicated than what I am looking for.

I don't need a random thing (although maybe you threw that is for a test.)

I just need an equation that basically says:

if the number of items in a list (x) =1-5, then round that number to 5
(x) = 6-10 round it to 10
(x) = 11-15 round it to 15
(x) = 16-20 round it to 20
and so on, up to any number, really.

There must be something simple, 5 lines or so that would do this.
thanks.
Alexis

Ali Guest 5. ## Re: Rounding numbers

Try this

on roundUp inputNum, toNearest
theMod = inputNum mod toNearest
if theMod = 0 then
return inputNum
else
extraRequired = toNearest - theMod
return inputNum + extraRequired
end if
end

--example usage
x = roundUp(theList.count, 5)

You will need to ensure than both parameters passed are integers, it won't work properly for floats

hth

johnAq

johnAq Guest 6. ## Re: Rounding numbers

Dude! that´s what the script does... the random thing is to test out the
script as I clearly commented there.

It works.

Arturo

"Ali Mac" <com> escribió en el mensaje
news:bu6746\$dk9\$macromedia.com...

Arturo Guest 7. ## Re: Rounding numbers

Arturo, sorry if I didn't understand your post.
I am reading these on the web, (not a usergroups reader), and for some reaosn your post came through with a bunch of odd html-ish tags that made it hard to read.

-Alexis

Ali Guest 8. ## Re: Rounding numbers

You actually made me think... hmmm "may be he needs not a handler but an
equation...." but I am not soooo smart!!! hahaha.... so I bet there´s a
way to create a one sentence equation for that but I am not very good at
math... :(

Hope it works though! Later! :)

Arturo

"Ali Mac" <com> escribió en el mensaje
news:bu6hj2\$1sc\$macromedia.com...
reaosn your post came through with a bunch of odd html-ish tags that made it

Arturo Guest 9. ## Re: Rounding numbers

"Arturo Toledo" <tv> wrote in message
news:bu6oth\$c6e\$macromedia.com...

It's a bit convoluted to get it to fit on one line, but:

on roundToN x, n
return ((bitOr(x,0)/bitOr(n,0))+(bitOr(x,0) mod bitOr(n,0)>0))*bitOr(n,0)
end

If you assume that you will always be passing integers then it looks
simpler:

on roundToN x, n
return (x/n)+(x mod n>0))*n
end

If you are prepared to go to a full 3 lines for the sake of efficiency and

on roundToN x, n
x = bitOr( x, 0 )
n = bitOr( n, 0 )
return (x/n)+(x mod n>0))*n
end

- Robert

Robert Guest 10. ## Re: Rounding numbers

Robert,

So, you can place a condition inside an expression...
How does this work?...

1. you divide x/n, ok... then
2. if x mod n is greater than 0 you add the result of that (x mod n) to
(x/n)???

Just to confirm... I´ve never used this technique. Pretty cool.

on roundToN x, n
return (x/n)+(x mod n>0))*n
end

Thanks!

Arturo

"Robert Tweed" <com> escribió en el mensaje
news:bu758t\$s88\$macromedia.com...
>
> It's a bit convoluted to get it to fit on one line, but:
>
> on roundToN x, n
> return ((bitOr(x,0)/bitOr(n,0))+(bitOr(x,0) mod[/ref]
bitOr(n,0)>0))*bitOr(n,0)

Arturo Guest 11. ## Re: Rounding numbers

"Arturo Toledo" <tv> wrote in message
news:bu77b3\$19e\$macromedia.com...

Yes, in Lingo true/false evaluate to 1/0, so for instance, if you put:

LHS = (x mod 5 > 0 )*5

Then if x mod 5 is zero, the part in brackets will evaluate to 0, so the
total is 0.

I x mod 5 is anything other than zero, the part in brackets will evaluate to
1, so the total is 5.

Note however, that just because this generally results in shorter code, when
it compiles into p-code, if you have a lot of these conditions it can result
in more (less efficient) code than if you used several lines of IF
statements to accomplish the same thing. Depends on what you're doing
though: in this case I believe it makes no difference whatsoever, so you
might as well have the simpler-looking code.

- Robert

Robert Guest 12. ## Re: Rounding numbers

it´s good to know.

Arturo

"Robert Tweed" <com> escribió en el mensaje
news:bu77qt\$1qt\$macromedia.com...
>
> Yes, in Lingo true/false evaluate to 1/0, so for instance, if you put:
>
> LHS = (x mod 5 > 0 )*5
>
> Then if x mod 5 is zero, the part in brackets will evaluate to 0, so the
> total is 0.
>
> I x mod 5 is anything other than zero, the part in brackets will evaluate[/ref]
to
when
result

Arturo Guest #### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•