Rounding integers (not floatround) | math help needed
#1

Hi

How I can round numbers like:

84 to 80,

178 to 180,

233 to 230?
Reply
#2

I'll write something for you. Just wait some minutes.

Check this out.
This way you can round integers to the next multiple of 10.
Like 5 will be 10, 3 will be 0.

When i divide 3 with 10, we'll have a remainder rem.
Using the mathematic principles of the euclidean division we can do it like this.


Code:
#include <a_samp>



main()
{
	
	new num = 5;
	new rem = num % 10;
        printf("%d",rem >= 5 ? (num - rem + 10) : (num - rem));

}
So a function can look like that.

Code:
round(num)
	{
		
		new num = 5;
		new rem = num % 10;
		return rem >= 5 ? (num - rem + 10) : (num - rem);
	}
So let's check this out.
We have our number 25.
25 / 10 = 2,5, alright we don't use it here, but it's important to understand.
We do 25 % 10 = 5, thats our remainder.

Now we check, is our remainder rem more than 5 or equal?
Our remainder is 5, so we'll subtract the remainder from our number and we'll add 10.
Means 25-5 = 20. 20+10 = 30. That's the math behind it.
Reply
#3

Quote:
Originally Posted by Y_Less
View Post
Simpler method:

Code:
a = CEILDIV(b, 10) * 10;
Simple. This is what I need. Thanks
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)