How can I shorten the decimals in this code?

Hi! I have a script that calculates how close you are until you can rank up, but I do not like how many decimals there are.

image
This is the picture, I want to be like 0.4%

Code:

local groupid = 7048567 -- Change this to your group ID, your group ID can be found by going to the group and copying the numbers after "https://www.roblox.com/My/Groups.aspx?gid=".

game.Players.PlayerAdded:connect(onPlayerRespawned)
function onPlayerRespawned(newPlayer)
	wait(1)
	if newPlayer then
		local gui=Instance.new("BillboardGui")
		gui.Parent=newPlayer.Character.Head
		gui.Adornee=newPlayer.Character.Head
		gui.Size=UDim2.new(4,0,2,0)
		gui.StudsOffset=Vector3.new(0,3,0)
		local texta=Instance.new("TextBox")
		texta.Size=UDim2.new(1,0,1,0)
		texta.BackgroundTransparency = 1
		texta.Font = Enum.Font.Legacy
		texta.TextScaled = true -- Set this to false if you want your tags to look like this http://prntscr.com/hrsqxw. It is highly recommended you change this to false if you have large group names.
		texta.Text = (newPlayer.Name .. " - " .. newPlayer:GetRoleInGroup(7048567)) .. "(loading%)" -- This is the format of what your tags will look like. This format currently will show "PLAYERNAME - GROUP RANK".
		texta.TextColor3 = Color3.new(255,0,0) -- When someone who is not in a group joins, this is the colour tag they will have.
		texta.Parent=gui
		if newPlayer:IsInGroup(4589206) then
			texta.TextColor3 = Color3.new(255,0,0) -- If a user IS in a group, this is the colour name tag that they will have.
		end
		while wait(0.5) do
			if newPlayer:GetRankInGroup(groupid) == 3 then
				texta.Text = (newPlayer.Name.. " - ".. newPlayer:GetRoleInGroup(7048567) .. "("..30*100/newPlayer.leaderstats.XP.Value.."%)")
				if newPlayer.leaderstats.XP.Value >= 30*100 then
					texta.Text = (newPlayer.Name.." - ".. newPlayer:GetRoleInGroup(groupid) .. "(100% ELIGIBLE TO RANK UP!)")
				end
			elseif newPlayer:GetRankInGroup(groupid) == 4 then
				texta.Text = (newPlayer.Name.." - ".. newPlayer:GetRoleInGroup(groupid) .. "("..80*100/newPlayer.leaderstats.XP.Value.."%)")
				if newPlayer.leaderstats.XP.Value >= 80*100 then
					texta.Text = (newPlayer.Name.." - ".. newPlayer:GetRoleInGroup(groupid) .. "(100% ELIGIBLE TO RANK UP!)")
				end
			elseif newPlayer:GetRankInGroup(groupid) == 5 then
				texta.Text = (newPlayer.Name.." - ".. newPlayer:GetRoleInGroup(groupid) .. "("..140*100/newPlayer.leaderstats.XP.Value.."%)")
				if newPlayer.leaderstats.XP.Value >= 140*100 then
					texta.Text = (newPlayer.Name.." - ".. newPlayer:GetRoleInGroup(groupid) .. "(100% ELIGIBLE TO RANK UP!)")
				end
			elseif newPlayer:GetRankInGroup(groupid) == 7 then
				texta.Text = (newPlayer.Name.." - ".. newPlayer:GetRoleInGroup(groupid) .. "("..200*100/newPlayer.leaderstats.XP.Value.."%)")
				if newPlayer.leaderstats.XP.Value >= 200*100 then
					texta.Text = (newPlayer.Name.." - ".. newPlayer:GetRoleInGroup(groupid) .. "(100% ELIGIBLE TO RANK UP!)")
				end
			elseif newPlayer:GetRankInGroup(groupid) == 8 then
				texta.Text = (newPlayer.Name.." - ".. newPlayer:GetRoleInGroup(groupid) .. "("..300*100/newPlayer.leaderstats.XP.Value.."%)")
				if newPlayer.leaderstats.XP.Value >= 300*100 then
					texta.Text = (newPlayer.Name.." - ".. newPlayer:GetRoleInGroup(groupid) .. "(100% ELIGIBLE TO RANK UP!)")
				end
			elseif newPlayer:GetRankInGroup(groupid) == 9 then
				texta.Text = (newPlayer.Name.." - ".. newPlayer:GetRoleInGroup(groupid) .. "("..410*100/newPlayer.leaderstats.XP.Value.."%)")
				if newPlayer.leaderstats.XP.Value >= 410*100 then
					texta.Text = (newPlayer.Name.." - ".. newPlayer:GetRoleInGroup(groupid) .. "(100% ELIGIBLE TO RANK UP!)")
				end
			elseif newPlayer:GetRankInGroup(groupid) == 10 then
				texta.Text = (newPlayer.Name.." - ".. newPlayer:GetRoleInGroup(groupid) .. "("..520*100/newPlayer.leaderstats.XP.Value.."%)")
				if newPlayer.leaderstats.XP.Value >= 520*100 then
					texta.Text = (newPlayer.Name.." - ".. newPlayer:GetRoleInGroup(groupid) .. "(100% ELIGIBLE TO RANK UP!)")
				end
			elseif newPlayer:GetRankInGroup(groupid) == 11 then
				texta.Text = (newPlayer.Name.." - ".. newPlayer:GetRoleInGroup(groupid) .. "("..650*100/newPlayer.leaderstats.XP.Value.."%)")
				if newPlayer.leaderstats.XP.Value >= 650*100 then
					texta.Text = (newPlayer.Name.." - ".. newPlayer:GetRoleInGroup(groupid) .. "(100% ELIGIBLE TO RANK UP!)")
				end
			elseif newPlayer:GetRankInGroup(groupid) == 12 then
				texta.Text = (newPlayer.Name.." - ".. newPlayer:GetRoleInGroup(groupid) .. "("..800*100/newPlayer.leaderstats.XP.Value.."%)")
				if newPlayer.leaderstats.XP.Value >= 800*100 then
					texta.Text = (newPlayer.Name.." - ".. newPlayer:GetRoleInGroup(groupid) .. "(100% ELIGIBLE TO RANK UP!)")
				end
			elseif newPlayer:GetRankInGroup(groupid) == 13 then
				texta.Text = (newPlayer.Name.." - ".. newPlayer:GetRoleInGroup(groupid) .. "("..1000*100/newPlayer.leaderstats.XP.Value.."%)")
				if newPlayer.leaderstats.XP.Value >= 1000*100 then
					texta.Text = (newPlayer.Name.." - ".. newPlayer:GetRoleInGroup(groupid) .. "(100% ELIGIBLE TO RANK UP!)")
				end
			elseif newPlayer:GetRankInGroup(groupid) == 14 then
				texta.Text = (newPlayer.Name.." - ".. newPlayer:GetRoleInGroup(groupid) .. "("..1300*100/newPlayer.leaderstats.XP.Value.."%)")
				if newPlayer.leaderstats.XP.Value >= 1300*100 then
					texta.Text = (newPlayer.Name.." - ".. newPlayer:GetRoleInGroup(groupid) .. "(100% ELIGIBLE TO RANK UP!)")
				end
			elseif newPlayer:GetRankInGroup(groupid) == 255 then
				texta.Text = (newPlayer.Name.." - ".. newPlayer:GetRoleInGroup(groupid) .. "("..1000*100/newPlayer.leaderstats.XP.Value.."%)")
			
			
			
			
			
			
			
				end
		end
	end
end

function onPlayerEntered(newPlayer)
	newPlayer.Changed:connect(function (property)
		if (property == "Character") then
			onPlayerRespawned(newPlayer)
		end
	end)
end

game.Players.PlayerAdded:connect(onPlayerEntered)

-- If you want to change the overhead name format so it only shows the players rank and not their
-- name too, at line 17 change the line to this; 
-- texta.Text = ("- " .. newPlayer:GetRoleInGroup(groupid)" -")

-- Or if you only want to show the players name, change line 17 to;
-- texta.Text = ("- " .. newPlayer.Name .. " -")

-- Alternatively, you can use your own format, but make sure you know what you're doing! 
-- Do not hesitate to message me if you need!

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

Could I have an example of that? It is kind of confusing.

1 Like
local value = 0.78452
value *= 100
value = math.floor(value)
value = value / 100

math.round is probably better than math.floor in this case so that it rounds it up or down and doesn’t just cut off the next digit.
value = math.round(value * 100) / 100

1 Like

They would both probably work the same, but math.round does seem better in this scenario.

I will try that one now, thank you!

There’s also string formatting.

E.g. print(("%.4f"):format(1/11))--"0.0909": the number after the dot controls the number of decimals.

2 Likes

Rounds the provided number to precision decimal places.

local function toFixed(num: number, precision: number, round: boolean?): number
    if precision == math.huge then
        return num
    elseif precision == 0 then
        return math[if round then "round" else "floor"](num)
    end

    local augment = 10 ^ precision
    return math.floor(num * augment + if round then 0.5 else 0) / augment
end

print(toFixed(0.4584939, 2)) -- 0.46

-- Or, in your case...
print(toFixed(0.4249984062259765, 1)) -- 0.4

This did work, but I have a question. will it still show the decimals or is there a way I could make it so when it’s at 99.5 it won’t say 100%? As I am making a progressive worker RolePlay game like Kavra’s Kingdom.

Use string.format. You can choose an exact number of decimal places:
print(string.format("%0.2f", 4/3)) --1.33
print(string.format("%0.3f", 4/3)) --1.333
print(string.format("%0.2f", 100)) --100.00

1 Like

Why use

local augment = 10 ^ precision
math.floor(num * augment + 0.5) / 10 ^ augment

when you can use

math.round(num * (10 ^ precision)) / 10 ^ precision

It works because you are multiplying by 100 (or whatever number of zeroes you want as decimal places), rounding it, then dividing it by 100.

So if you start with 14.2594 as an example
it is multiplied by 100 which equals 1425.94,
which gets rounded to 1425,
and then gets divided by 100,
so it equals 14.25.

There was a typo in my code (when changing the two 10 ^ precision instances to a variable), that’s all. The + 0.5 is to perform rounding, if you don’t want it to round then remove it.

That being said, I will edit my original post to allow for toggling the rounding.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.