Rounding to nearest multiple
#1

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.
Reply
#2

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
Reply
#3

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
Reply
#4

PHP код:
CODE 
Reply
#5

Returns 0.0
Reply
#6

some missing Float tags

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

Reply
#7

Thanks a lot!
Reply
#8

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.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)