# Round a number to the nearest multiple of another number

Hello world.

I have been trying to find some code that lets me round a number to the nearest multiple of another number.

So if for example we have number 44 the nearest multiple of seven would be 42.

Anyone knows how to achieve this?

Thank you very much!

I don’t know any ActionScript, but I know a solution to your problem.

Some pseudocode:

``````VARIABLE MYVAR = 44;
VARIABLE TEMP = 0;
VARIABLE MULTIPLE = 7;

WHILE (TEMP <= MYVAR) DO
IF (MYVAR < (TEMP + (MULTIPLE / 2)) THEN
RETURN TEMP
ELSE
TEMP = TEMP + MULTIPLE
END WHILE
``````
``````var a=7;
var b= Math.floor(44/a); //6
var c= a*b; //42
trace(c);
``````

Legends!

This returns 42 for 48, which is not the nearest multiple of 7. Is there a Math.round() in actionscript?

Yes you’re right Dan. I thought that’s what he is asking. You get 49 with Math.round()

Actually what I need to do is round it to the next highest multiple and if it’s equal just leave it there. just realized. Great help there, thanks. Still trying to figure out that one.

Actually I’m getting a bit odd behavior. Neather math.floor or math.round make it for my case.
I have a varying number of thumbnails each time, arranged in 7 columns. I want to draw a dynamic rectangle with a height based on the number of rows that I get.

Try Math.ceil() in this case or just use Math.floor() and add 1.

``````
var columns=7;
var thumbnails=44;
var rows= Math.ceil(thumbnails/columns);

trace(columns, rows, thumbnails);
``````

Math.ceil() did it. Thank you!