Page 1 of 1

[SOLVED]Round a number to the nearest multiple..

PostPosted: Sat Nov 05, 2011 2:20 pm
by wilfryed
Het all!

Today it's about Maths, and well my brain crash down when it's about Maths.

Here's the problem:

I've a first number whose value is determined by the X position of the mouse. (We call it a)
I've a second number who is always even. (We'll call b)

a and b are integer.

How to round up or down a to the nearest multiple of b?

If someone wanna help me, or teach me!wilfryed2011-11-05 15:29:15

Re: [SOLVED]Round a number to the nearest multiple..

PostPosted: Sat Nov 05, 2011 2:26 pm
by Mipey
RoundedX = round( MouseX / GridX ) * GridX

Basically, first you divide the mouse X position by that even number, then you multiply it by that even number.

In some cases you may want to use floor or ceil instead of round (for rounding up or down).

Re: [SOLVED]Round a number to the nearest multiple..

PostPosted: Sat Nov 05, 2011 3:27 pm
by wilfryed
Hurray!

You're my favorite mathematician ahah!