SA-MP Forums Archive
Rounding to nearest multiple - 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 to nearest multiple (/showthread.php?tid=614518)



Rounding to nearest multiple - AMouldyLemon - 09.08.2016

I need an equation which returns the nearest value to a specified multiple. I've tried searching around the web but could only find either Rounding up or Down. I've been trying to find a solution to this problem for ~5 days, any help would be much appreciated, thanks.


Re: Rounding to nearest multiple - PrO.GameR - 09.08.2016

A - Divide the number by the specified number (that you want it to be multiple of) (rather as a float/float so you would get precision)
B - Use floatround
C - multiply the number by specified number


Re: Rounding to nearest multiple - AMouldyLemon - 09.08.2016

That returns the nearest whole number.

Код:
public Align(Float:angle) {
	new Float:a = angle / 45;
	floatround(a);
	return a * 45;
}
EDIT:
It actually just prints the number given in the parameter. Ex: 34.4 prints 34.399999


Re: Rounding to nearest multiple - Nero_3D - 09.08.2016

PHP код:
CODE 



Re: Rounding to nearest multiple - AMouldyLemon - 09.08.2016

Returns 0.0


Re: Rounding to nearest multiple - Nero_3D - 09.08.2016

some missing Float tags

PHP код:
FloatAlign(FloatangleFloatnumber 45.0) {
    return 
floatround(angle number) * number;




Re: Rounding to nearest multiple - AMouldyLemon - 09.08.2016

Thanks a lot!


Re: Rounding to nearest multiple - PrO.GameR - 09.08.2016

floatround(a); doesn't change a to a rounded one, it returns one with rounded, so you would need to actually assign that value to something, like new temp=floatround(a);
PHP код:
public Align(Float:angle)
    return 
floatround(angle/45.0)*45
Or if you want a more general function:
PHP код:
public Align(Float:anglenumber=45)
{
    new 
num=floatround(angle/float(number));
    return 
num*number;

This gotta work, however note that most important part of that is the 45.0, because it makes it a float/float division so 34.4 would actually return 45 instead of 0




EDIT: Open tabs didn't notice he answered.