Leaderstats GUI adding unneeded decimals

For some reason the script I made to have a counter GUI for their leaderstats creates unneeded decimals, which is makes it a long string of numbers which shouldn’t be there.
Screen Shot 2021-12-26 at 1.26.12 PM
If anyone could help fix this that would be great.

local function Format(value, idp)
	local ex = math.floor (math.log(math.max(1, math.abs(value)), 1000))
	local abbrevs = abbrev [1 + ex] or ("e+"..ex)
	local normal = math.floor (value * ((10 ^ idp) / (1000 ^ ex))) / (10 ^ idp)
	return ("%"..idp.."f%s"):format(normal, abbrevs)

while wait() do
	local player = game.Players.LocalPlayer
	script.Parent.Text = Format(player.leaderstats.Kills.value,2)

Use “%.2f” to force exactly two digits after the decimal separator:

print(("%.2f"):format(64)) -- 64.00
print(("%.2f"):format(64.006)) -- 64.01
print(("%.0f"):format(64.4) -- 64

or alternatively use “%g” to use the most appropriate/pretty formatting:

print(("%g"):format(0.00000064)) -- 6.4e-07
print(("%g"):format(6491000001)) -- 6.4e+09
print(("%g"):format(64)) -- 64
print(("%g"):format(64.0001)) -- 64.0001

You can read more about the modifiers here: printf, fprintf, sprintf, snprintf, printf_s, fprintf_s, sprintf_s, snprintf_s - cppreference.com

Yes, it’s for the C standard library:

The format string follows the same rules as the printf family of standard C functions. The only differences are that the options/modifiers * , l , L , n , p , and h are not supported and that there is an extra option, q . The q option formats a string in a form suitable to be safely read back by the Lua interpreter: the string is written between double quotes, and all double quotes, newlines, embedded zeros, and backslashes in the string are correctly escaped when written.
- Lua 5.1 Reference Manual, string.format (formatstring, ···)

I know exactly why this is happening, in your return statement you say

It should be

return ("%."..idp.."f%s"):format(normal, abbrevs)

You were missing the decimal after the first percent sign.

Say did you happen to get this script from my video

He was missing the decimal, it is using the idp variable passed in the function as an easy way to change the amount of decimals.

This is a video where I make this exact script

this is awkward, to me at least

and yeah, I did