Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How do I round a series of numbers to the closest, for example 50?

I have a series of prices in a table that is calculated automatically based on an average health which leaves numbers such as 142, 174, 229, 4320.

for i, v in pairs(priceTbl) do
	local avgHealth = (v["Health"][1] + v["Health"][2] + v["Health"][3])/3
	v["Price"] = math.floor(avgHealth * 1.3)
end

How does one achieve making it so that when the raw price is calculated, it gets rounded to 50?

2 comments

0

I believe this only rounds to the closest 1?

1 answer

✓ Accepted answer

Divide/multiply a number before putting it through math.floor:

function round(n, toNearest)
	toNearest = toNearest or 1
	return math.floor(n / toNearest + 0.5) * toNearest
end
print(round(5.3)) -- 5
print(round(122, 50)) -- 100
print(round(125, 50)) -- 150

1 comment

0

thank u kind sir!

This discussion is preserved as it was. New answers, comments, and votes are closed.