SA-MP Forums Archive
Rounding integers (not floatround) | math help needed - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+--- Thread: Rounding integers (not floatround) | math help needed (/showthread.php?tid=663298)



Rounding integers (not floatround) | math help needed - m4karow - 27.01.2019

Hi

How I can round numbers like:

84 to 80,

178 to 180,

233 to 230?


Re: Rounding integers (not floatround) | math help needed - faxxe - 27.01.2019

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.


Re: Rounding integers (not floatround) | math help needed - m4karow - 27.01.2019

Quote:
Originally Posted by Y_Less
View Post
Simpler method:

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