How do I round a decimal?

Hello!

I am trying to have a script detect when there’s a sale, the issue is that I use numbers that aren’t easily rounded which causes lots of decimals to appear. While it technically isn’t correct, it’s not appealing. Is there a way to easily solve this without adjusting my prices?
I’ve looked it up and Lua doesn’t have a math.round feature so that isn’t an option unfortunately.

Here’s my script and the result:

local MPS = game:GetService("MarketplaceService")

local Gamepasses = {
	["VIP"] = {
		["GamepassID"] = 10587779,
		["DefaultPrice"] = 199
	},
	["Double"] = {
		["GamepassID"] = 10587903,
		["DefaultPrice"] = 99
	}
}

while true do
	for i,v in pairs(Gamepasses) do
		local price = MPS:GetProductInfo(v.GamepassID, Enum.InfoType.GamePass).PriceInRobux
		for i2,v2 in pairs(script.Parent.Gamepasses[i]:GetDescendants()) do
			if v2.Name == "Price" and v2:IsA("TextLabel") then
				v2.Text = "R$"..price
				if price <= v.DefaultPrice then
					local sale = script.Parent.Gamepasses[i].SaleLabel
					sale.Visible = true
					sale.Text = (1 - (price / v.DefaultPrice)) * 100 .."% OFF!"
				end
			end
		end
	end
	wait(300)
end

image

math.floor to round down? math.ceil to round up? If you want proper rounding you can do math.floor(n + 0.5) or math.ceil(n - 0.5)

3 Likes

The math.floor variant is the correct one, as your ceil operation after subtraction would round 1.5 to 1 instead of 2.

4 Likes

it is really simple just use math.floor()

I think it would still cause some inaccuracies as another price, which is 199, if it is on sale at 149, it would say it’s 26% off when in reality it’s 25. I think using both ciel and floor provides a more accurate description, I don’t really mind saying 51% off instead of 50% off either really.