How do I make it so there's only one decimal point?

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    Limit the amount of decimals in a string.

  2. What is the issue? Include screenshots / videos if possible!
    I don’t know how

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    Searching on google.

I would like to limit the decimal points to only show one decimal point (example: 0.5 instead of 0.571)

local char = game.Players.LocalPlayer.CharacterAdded:Wait()

local humanoid = char.Humanoid

while task.wait() do
	local textlabel = game.Players.LocalPlayer.PlayerGui.MaxHP.MaxHP
	textlabel.Text = "Max HP: "..humanoid.MaxHealth
end

You can use a string pattern to do this. The string pattern for 1 decimal point is %.1f, so:

local function limitTo1Dp(num: number): string
    return string.format("%.1f", num)
end

--you can cast it back to a number afterwards if you want
print(limitTo1Dp(5.352)) --> 5.4

Just a quick tip, you should NEVER use while loops when it comes to tasks like these, you should either use RunService.RenderStepped() or Humanoid.HealthChanged.

It works, thanks. I guess. It shows the decimal even if it’s 0 but it dosen’t really bother me

1 Like

Thanks i’ll make sure to change it…

1 Like

in that case, you can add a gsub statement afterwards to remove it:

local function to1Dp(num: number): string
     local formatted = string.format("%.1f", num)
     formatted = formatted:gsub("%.0", "") --this works specifically to 1 d.p., please note
     return formatted
end