How to remove trailing 0s after a decimal?

Hey! I’m wondering how I remove trailing 0s after a decimal, heres an example.

0.100000 → 0.1
50.4000 → 50.4
0.000010000 → 0.00001

(Should use a simple gsub or something.)

Thanks very much!

As a number this should be done automatically… can you show how you are using it?

local s1 = "0.100000"
local s2 = "50.4000"
local s3 = "0.000010000"

local function truncateTrailingZeros(s : string) : string
	local _s = string.gsub(s, "0*$", "")
	return _s
end

print(truncateTrailingZeros(s1)) --0.1
print(truncateTrailingZeros(s2)) --50.4
print(truncateTrailingZeros(s3)) --0.00001
1 Like

Are you working with strings or numbers? If it’s numbers, use the %g formatting mode (pretty printing).

print(("%g"):format(0.1)) -- 0.1
print(("%g"):format(0.00100)) -- 0.001