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.
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
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?
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
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"
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
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)
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
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
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