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

I don't know how to lower the amount of decimal points?

math.randomseed(tick())


for _ = 1, 10 do
	while true do
			script.Parent.SurfaceGui.Frame.TextLabel.Text = "$" .. math.random()*100 .. math.floor()*100/100
			wait(25)
	end
end

i've been trying to get this to round the massive decimals to only 2 decimal places instead of like 15 decimal places, but i can't get it to work. i'm still very new to scripting so i'm not the best at it.

1 answer

String Interpolation

You can use string.format. A lot of people actually don't know this, but the format function can define the precision of any given number, given some float, to however many decimal places you want. Here's an example...

local function round_number(n, places)
	return tonumber(string.format("%." .. (places or 0) .. "f", n))
end

The format goes as follows: %. says that you are looking for precision, .. places .. is an integer specifying how precise the number should be, f says it's a float. You would call it as such...

round_number(10.9482, 2) -- Round to 2 decimal places

10.95

I don't have a lot of time right now, so I'm not able to add more telling details to my answer. However, you can read more about it for yourself on this wiki page, and I'm sure you can find more information about format on google or this website. I'd also recommend learning what a floating point number is, just for extra information if you're interested.

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