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

Get help using Construct 2

### » Sun Jun 12, 2016 6:00 am

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?
B
80
S
41
G
13
Posts: 257
Reputation: 13,511

### » Sun Jun 12, 2016 7:05 am

Unconnected wrote: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

Multiples of 10:
0, 10, 20 , 30, 40
16 would = 20

int(x/10)*10

Multiples of 32:
0, 32, 64, 96, 128
89 would = 96

int(x/32)*32

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

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

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.
B
27
S
3
G
1
Posts: 497
Reputation: 2,349

### » Sun Jun 12, 2016 7:51 am

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.
Last edited by Unconnected on Sun Jun 12, 2016 11:01 am, edited 1 time in total.
B
80
S
41
G
13
Posts: 257
Reputation: 13,511

### » Sun Jun 12, 2016 8:09 am

All covered in the Manual.
If your vision so exceeds your ability, then look to something closer.
Moderator
B
145
S
33
G
89
Posts: 5,921
Reputation: 62,607

### » Sun Jun 12, 2016 9:18 am

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.
Last edited by Unconnected on Thu Jun 16, 2016 12:41 am, edited 1 time in total.
B
80
S
41
G
13
Posts: 257
Reputation: 13,511

### » Sun Jun 12, 2016 10:50 am

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.
B
80
S
41
G
13
Posts: 257
Reputation: 13,511

### » Sun Jun 12, 2016 8:23 pm

Unconnected wrote: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
Game design is all about decomposing the core of your game so it becomes simple instructions.
B
54
S
23
G
18
Posts: 2,123
Reputation: 17,200

### » Thu Jun 16, 2016 12:17 am

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?
B
80
S
41
G
13
Posts: 257
Reputation: 13,511

### » Thu Jun 16, 2016 1:30 am

The % operator gives the remained after a division.
Ex
12%10 = 2
B
100
S
38
G
134
Posts: 5,556
Reputation: 85,325

### » Thu Jun 16, 2016 1:51 am

@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!)
B
99
S
35
G
20
Posts: 438
Reputation: 17,960

Next