How can I abbreviate the numbers (or something like that)

Hi, basically I want to shorten the numbers without affecting the number directly, for example:
0.43445434 becomes only 0.43. I don’t know if shortening is the correct word for what I want to do, I just want to shorten the numbers. The following script is to make a SliderGui of anything, only that I want to add a winged text that takes the number of X out of the position, only that it is too long and I want to shorten it (or is it abbreviated?).

local Button = script.Parent:FindFirstChild("ImageButton")
local Fondo = script.Parent:FindFirstChild("Fondo")
local Nivel = script.Parent:FindFirstChild("Nivel")

local TweenService = game:GetService("TweenService")
local UserInputService =  game:GetService("UserInputService")

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Fire = ReplicatedStorage:WaitForChild("ConfiguracionGeneral"):WaitForChild("Change_configuration")

local text = script.Parent.Parent:WaitForChild("NivelDeBrillo")

local Enabled = false
local Debounce = false

local TimeWait = 0.05

local tweenInfo = TweenInfo.new(TimeWait)

Button.MouseButton1Down:Connect(function()
	if not Debounce then
		Debounce = true
		wait(TimeWait)
		Enabled = true
		Debounce = false
	end
end)

UserInputService.InputChanged:Connect(function()
	if Enabled == true then
		local MousePos = UserInputService:GetMouseLocation() + Vector2.new(0, 36)
		local RelvPos = MousePos-Fondo.AbsolutePosition
		local Precent = math.clamp(RelvPos.X/Fondo.AbsoluteSize.X, 0, 1)
		Button.Position = UDim2.new(Precent, 0, 0, 5)
		Nivel.Size = UDim2.new(Precent, 0, 0.88, 0)
	end
end)


UserInputService.InputEnded:Connect(function(input)
	if input.UserInputType == Enum.UserInputType.MouseButton1 and Enabled == true then
		Enabled = false
		text.Text = Button.Position.X.Scale
		print(Button.Position.X.Scale)
		--Fire:FireServer("BrilloVol", "BrilloVolumen10")
	end
end)

Truncate the number. This is simple.

math.floor(x) will remove the decimals from x, so, if x = 1.234 then math.floor(x) is 1.

Expanding on this, we will take x and multiply it by a power of 10. This will shift the decimal place of x, so that when rounded, you can divide by that power of 10 and receive a truncated version of your original number.

function truncate(x, p)
    local y = 10^p
    return math.floor(x * y) / y
end

Example of how this evaluates

x = 1.234
p = 2

x_t = truncate(x, 2)
    -- = math.floor(x * 10^2) / 10^2 -- shift decimal by 2 places (multiply 100)
    -- = math.floor(123.4) / 100 -- now 4 gets shaved off after math.floor
    -- = 123 / 100 
    -- = 1.23 -- truncated to 2 decimals

print(x_t) -- 1.23
1 Like

I think I miss math class. Well, I’ll see how to apply it