Rouding Numbers?

So let’s say I have the number 1.23333333, I want it to round it to 1.23,
but if I have 1.263333333333 I want it to round to 1.26,
but if I have 0.000003 I want it to round to 0.000003 just the same,
but with 1.000003 I want it to round to 1 almost the same.

I know it sounds weaird, but how do I make a rounding system like this.

3 Likes

What do you plan to use the rounded numbers for?

1 Like

I’m making a precent system but when I do it I use low number so it gos to like 1.3333333333333333333333 I dont want that but it needs to round in a special way

1 Like

I believe this could help you for first 3 examples:

but the 4th one contradicts the first 3, do you want it to round up to only 2 decimals if the number is 1 or higher and round up differently when its smaller than 1?

1 Like

local roundedNumber = Math.round(value * 100) / 100

1 Like

I want it to round diffrently when its lower than 1 and above 1 just 2 decimals

1 Like

function customRound(num)
if num >= 1 then
return math.floor(num * 100 + 0.5) / 100 – Rounds to 2 decimal places for numbers >= 1
else
return num – Keeps the exact value for numbers < 1
end
end

1 Like

Then you can have number round like above code for when its lower than 1 and if its 1 or above just do this instead: string.format("%.2f", num)

1 Like

You could also just do this:

local function RoundNumber(number: number, places: number): number
   return math.floor(number * (10^places)) / (10^places)
end

print(RoundNumber(1/3, 2)) --> prints "0.33"
print(RoundNumber(math.pi, 4)) --> prints "3.1415"
print(RoundNumber(1.4627365728, 1)) --> prints "1.4"
2 Likes

wait one more thing its not a string

1 Like

You still need to use tonumber() on the string.format() to turn it back into a number.

1 Like

idk if this is alright but im always rounding like this

local Number = 1.2

print(math.round(Number))
1 Like

What I’m trying to say is that my number never was a string and because of that I get this error

.PlayerGui.MainGui.Background.ChanceTextBox.ChanceScript:12: attempt to get length of a number value

btw I’m trying to make it into 1 function so I can just do:

local chance = tonumber(string.format("%.".. round(chance)+2 .."f", chance))

or even better

local chance = round(chance)

and than the function should look something like this

local function round(chance)
	if chance >= 1 then
		string.format("%.2f", chance)
	else
		local zeros = 0
		local decpoint = string.find(chance,"%.") or 0

		for i = decpoint + 1, #chance do
			if string.sub(chance,i,i) ~= "0" then break end
			zeros += 1
		end

		return zeros
	end
end

pls help

1 Like
local function round(chance)
    if chance >= 1 then
        return string.format("%.2f", chance)
    else
        local str_chance = tostring(chance)
        local zeros = 0
        local decpoint = string.find(str_chance,"%.") or 0

        for i = decpoint + 1, #str_chance do
            if string.sub(str_chance,i,i) ~= "0" then break end
            zeros += 1
        end
        return zeros
    end
 end
local chance = round(chance)
1 Like

It would be easier if you just leave checkzeros as its own function and for round function just do:

local function round(chance)
    if chance >= 1 then
        return tonumber(string.format("%.2f", chance))
    else
        return tonumber(string.format("%.".. checkzeros(chance)+2 .."f", chance))
    end
end
2 Likes

this is perfect, but I get this error:
Players.pasje1312.PlayerGui.MainGui.Background.ChanceTextBox.ChanceScript:9: attempt to get length of a number value - Client - ChanceScript:9

Can you send the part where error occurs?

local function checkzeros(chance)
	local zeros = 0
	local decpoint = string.find(chance,"%.") or 0

	for i = decpoint + 1, #chance do
		if string.sub(chance,i,i) ~= "0" then break end
		zeros += 1
	end

	return zeros
end

local function round(chance)
	if chance >= 1 then
		return tonumber(string.format("%.2f", chance))
	else
		return tonumber(string.format("%.".. checkzeros(chance)+2 .."f", chance))
	end
end

line 4`and btw the chance number is not a string its a number

You are using the modified checkzeros, try replacing it with original one.

I gotta go for like half an hour, can you explain waht you mean with your reply.