Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 5 years ago

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?

0
I believe this only rounds to the closest 1? Marmalados 193 — 5y

1 answer

Log in to vote
2
Answered by 5 years ago

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
0
thank u kind sir! Marmalados 193 — 5y
Ad

Answer this question