How to round to 2 Decimal places?

I got a Value that = 0.984375, and I want it to be rounded to 2 decimals: 0.98
How would I do this?


You could just make a new string of the num, using string.sub, then convert it back to a number

For example,

local number = 0.984375 --original number

local newnumber = string.sub(tostring(number), 1,4)
newnumber = tonumber(newnumber)

To make this a function, do the following:

local function convert(num)
    local newnumber = string.sub(tostring(num), 1,4)
	newnumber = tonumber(newnumber)


There’s probably better ways of doing it, but that’s just 1 way. I would recommend using @SuperSpeedy101’s way, because it’s more efficient and easier.


Multiply the value by 100
then use math.floor(value)
then divide by 100


local value = 0.984375
value *= 100
value = math.floor(value)
value = value / 100

I have an alternative to superspeedy101’s answer:


it works, i tested it.


Yes that does work, however math.round is rounding (either doing floor or ceil depending on the decimals) which might have a performance cost and possibly floating point jank, but overall it seems to work the same.

In summary:

Using Luau: math.round(number*100)/100 or math.floor(value*100)/100

Using just Lua: math.floor(value*100)/100 (There is no math.round in vanilla lua)

1 Like

i see. the only differece is that normal lua doesnt use “math.round” in it’s code.

1 Like

i think i made or pasted a function a while ago to round numbers by decimal places

function math_round(n: number, scale: number?)
    return tonumber(string.format("%." .. (typeof(scale) == "number" and scale or 2) .. "f", n))

not sure why math.round doesn’t have a second param by default


sorry to necro, but do you know how i can get this rounded number without a decimal, using the output for an ID but i cant have a decimal, just the numbers

Confusing but, simple


rounds a number without any decimals

i want to take the number and just remove the .

example, i have 5.3892384937, it goes to 5.389, this then goes to 5389

Here’s my function to round numbers to whatever decimal places you want them to.

local function RoundNumber(Number, NumberDecimalPlaces)

	local Rounding = math.floor

	local Digits

	if Number < 0 then
		Number -= (1/(10^(NumberDecimalPlaces+1)))
		Digits = math.ceil(math.log10(math.clamp(Number *-1,0,math.huge)))
		Rounding = math.ceil
		Number += (1/(10^(NumberDecimalPlaces+1)))
		Digits = math.ceil(math.log10(math.clamp(Number,0,math.huge)))

	local Result = Rounding(Number*(10^NumberDecimalPlaces))/(10^NumberDecimalPlaces)

	return Result 


1 Like

My initial just thought to do this would be convert it to a string, remove the decimal from the string, and then convert it back to a number.

Also, if you would like to continue this conversation, please open a new thread or go to DMS

textstring.format("%.f2", yourstring)
--[[                  ^
Allows however many decimals that the number is.

A simple solution.