Professional Web Applications Themes

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. #1

    Default 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. #2

    Default Re: Rounding numbers

    property myList

    on randomList - Generate random number list
    myList = []
    repeat with i = 1 to random(100)
    add myList,i
    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. #3

    Default 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)
    add myList,i
    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. #4

    Default 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. #5

    Default 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. #6

    Default 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. #7

    Default 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. #8

    Default 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
    hard to read. 


    Arturo Guest

  9. #9

    Default 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
    readability, then you can have:

    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. #10

    Default 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. #11

    Default 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. #12

    Default 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

Similar Threads

  1. rounding
    By GeorgeWS in forum Coldfusion - Advanced Techniques
    Replies: 2
    Last Post: August 10th, 09:05 AM
  2. Rounding of floating point numbers
    By Robert in forum PERL Beginners
    Replies: 18
    Last Post: February 25th, 10:59 AM
  3. Rounding off
    By Mike Blezien in forum PERL Beginners
    Replies: 1
    Last Post: November 22nd, 11:22 PM
  4. Rounding Up
    By Brent Bortnick in forum ASP
    Replies: 14
    Last Post: October 8th, 04:19 AM
  5. SQL Rounding to 6 decimals instead of 8
    By Gert-Jan in forum Microsoft SQL / MS SQL Server
    Replies: 5
    Last Post: July 15th, 06:03 PM

Bookmarks

Posting Permissions

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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139