How do I round a number to multiple of another number?

0 favourites
  • 13 posts
From the Asset Store
Be quick and choose the right answer for the shown equation.
  • Lets say I need a number to be rounded to the nearest multiple of 5, 10, 32... etc

    Examples:

    Multiples of 5:

    0, 5, 10, 15, 20 ,25

    12 would = 10

    Multiples of 10:

    0, 10, 20 , 30, 40

    16 would = 20

    Multiples of 32:

    0, 32, 64, 96, 128

    89 would = 96

    What about rounding up or down and not to nearest? As in:

    Rounding down to make 14 = 10 or

    Rounding up to make 11 = 15?

    I am aware I may have to create a variable, just not sure if there is an expression for this already or if I have to take the number and put it in a formula.

    Also will Round(5.5) = 5 or 6?

    I have only read 5.4 = 5 and 5.6 = 6 in forum examples.

    I never read what would happen if it was in the exact middle.

    Does it just pick up or down randomly?

    How sensitive is Ceil and Floor?

    would floor(5.99999) still be 5?

    would ceil(5.00001) still be 5?

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • Lets say I need a number to be rounded to the nearest multiple of 5, 10, 32... etc

    Examples:

    Multiples of 5:

    0, 5, 10, 15, 20 ,25

    12 would = 10

    int(x/5)*5 that would be from your example int(12/5)*5 <-> int(12/5)*5 <-> int(2.4)*5 -> 2*5=10

    Try it yourself with a textbox:

    System| On start of layout -> Text| set text to int(12/5)*5

    [quote:1s4v1dec]

    Multiples of 10:

    0, 10, 20 , 30, 40

    16 would = 20

    int(x/10)*10

    [quote:1s4v1dec]

    Multiples of 32:

    0, 32, 64, 96, 128

    89 would = 96

    int(x/32)*32

    [quote:1s4v1dec]

    What about rounding up or down and not to nearest? As in:

    Rounding down to make 14 = 10 or

    Rounding up to make 11 = 15?

    floor(14/10)*10

    ceil(11/10)*10

    [quote:1s4v1dec]

    I am aware I may have to create a variable, just not sure if there is an expression for this already or if I have to take the number and put it in a formula.

    Also will Round(5.5) = 5 or 6?

    I have only read 5.4 = 5 and 5.6 = 6 in forum examples.

    I never read what would happen if it was in the exact middle.

    Does it just pick up or down randomly?

    Why have you tried it yourself?

    round(5.5) is 6

    [quote:1s4v1dec]

    How sensitive is Ceil and Floor?

    would floor(5.99999) still be 5?

    would ceil(5.00001) still be 5?

    Same here try it yourself.

    floor(5.99999) is 5

    ceil(5.00001) is 6

    That is what they are for.

  • I saw a "%" symbol used before and wasn't sure what it meant. I didn't know int() even existed, figured that was what ceil and floor was for.. Still a bit new. 5.5 is kind of half full half empty kind of thing. Wasn't sure if there was a built in expression or not, this answers it. Thank you.

  • All covered in the Manual.

  • Nothing that I asked in this post can be found in the Manual. I asked for a formula or for a built in expression. It wasn't in the manual so I wasn't sure. When rounding 5.5 is in the middle and can technically be rounded up or down. Was asking if Ceil and Floor was hard limit or not. 5.9 and 5.9999 are different. One has 10% missing while the other has .0001% missing.

    Int is in the manual. I didn't know the use of it at the time, so reading it and it sticking with me are two different things. I will admit I misread your post at first. I hope it didn't come across as me being a jerk.

  • The "int(x/10)*10" Will always round down. 19/10 is 1.9 so it gets converted to 1 and then the answer is 10.

    You did answer my question though.

    "round(x/10)*10" rounds to nearest (x.5 is rounded up)

    "ceil(x/10)*10" rounds up. (1.000000000000000001 = 2)

    "floor(x/10)*10" rounds down (1.999999999999999 = 1)

    This brings up another question, What is "int()" for if "floor()" already does that?

    I saw that if there is text after the numbers it can get the numbers.

    "33xx" would = 33

    but "xx33" would = 0

    What would a reason be that letters and numbers are mixed up?

    I have never come across a reason for this.

    The only thing I can think of is X,Y coordinates, but I have always had a variable to go by.

  • Nothing that I asked in this post can be found in the Manual. I asked for a formula or for a built in expression. It wasn't in the manual so I wasn't sure, plus sometimes someone doesn't know how to look up what they don't know. When rounding 5.5 is in the middle and can technically be rounded up or down. Was asking if Ceil and Floor was hard limit or not. 5.9 and 5.9999 are different. One has 10% missing while the other has .0001% missing.

    Sorry for the quite useless input, but rounding 5.5 will give you 6 for equity purpose

    5.0;5.1;5.2;5.3;5.4 gives 5 after rounding

    5.5;5.6;5.7;5.8.5.9 gives 6 after rounding

    also I think int gets the integer part of a number, which is different from floor for negative numbers.

    also, you shouldn't really use int on strings, it works in the case you described but this is more an "thing that happens" rather than a feature

  • TileXValue >= 0 ? (TileXValue - TileXValue%64) : (TileXValue - TileXValue%64) -64

    TileYValue >= 0 ? (TileYValue - TileYValue%64) +64 : (TileYValue - TileYValue%64)

    This is what I was talking about. I believe it rounds up by 64s. Can someone explain this?

    I would like to know what the "%" symbol does. I assume it has to do with percent, but in programming symbols don't always mean what they mean to normal everyday people.

    To me it looks like a If-Then-Else, but each one is in a "Value" field for a "Set Value".

    I didn't know this could be done.

    What the above formulas are seemingly doing is rounding up to the nearest 64, and they have a built in offset.

    Would "ceil(x/64)*64" be similar (except for the offset)?

    Why would somebody complicate a formula like this, or am I misunderstanding the longer formula?

  • The % operator gives the remained after a division.

    Ex

    12%10 = 2

  • Unconnected

    The % symbol stands for MOD or modulo, which gives a remainder after dividing, so 197 % 64 = 5

    because 64 divides into 197 three times with 5 left over.

    TileXValue >= 0 ? (TileXValue - TileXValue%64) : (TileXValue - TileXValue%64) -64

    this is an if then else statement. The ? is the THEN and the : is the ELSE

    For what you are trying to do , I would use: Round(number/multiple) * multiple

    anything .5 or above will round up, and anything .499999... or down will round down.

    (oh! ROJOhound beat me to it again!)

  • Can someone tell me if this is correct?

    If TileXValue = 12

    TileXValue >= 0 ? (TileXValue - TileXValue%64) : (TileXValue - TileXValue%64) -64

    12 >= 0 Then (12 - 4) Else (12 - 4) - 64

    12 >= 0 Then 8 Else -56

    So the answer would be 8?

  • Unconnected

    12 % 64 = 12

    because 12 goes into 64 zero times with 12 left over.

    so, you would have 12>=0 Then (12 - 12) else (12 - 12) - 64

    which is 0 or -64

    0 is the answer.

  • I did the math wrong then...

    What I did was put 12 into 64 five times (60) with 4 left over.

    I thought my answers seemed off though.

    Basically it subtracts 64 from TileXValue until it can't anymore and what is left is the answer?

    So it means...

    64 will go into 12 Zero times, since there is 12 left over the answer is 12.

    so then

    64 will go into 68 One Time with 4 left over. So the answer would be 4.

    Thanks for your time.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)