UserFunction; round - HWRM/KarosGraveyard GitHub Wiki

round(<num>, <nearest_n>)

Description

Rounds num to the nearest_n (which is 1 by default), which can be any number value including fractions. Negative 'nearest' targets are identical to passing their absolute values.

Arguments

Param Type Description
num number The original number to be rounded.
nearest_n number? The value to round num 'to', i.e round(17.62, 0.5) = 17.5. Default 1.

Example

print(round(165, 100)); -- 200
print(round(149, 100)); -- 100

print(round(7.2309, 0.001)); -- 7.231 (rounded up `09` to `10`)

print(round(15.27, 0.25)); -- 15.25
print(round(15.27, 0.5)); -- 15.5
print(round(15.27, 15)); -- 15

Declaration

function round(num, nearest_n)
	nearest_n = nearest_n or 1;

	if (nearest_n == 1) then
		return floor(num + 0.5);
	end

	return round(num / nearest_n) * nearest_n;
end

Related Functions

⚠️ **GitHub.com Fallback** ⚠️