Remove Decimals (without rounding)

Hey.

I’m wondering how I could turn a digit from something like 372.837282 into 372837282 without the decimal point.

I’ve tried using gsub, sub and I haven’t seemed to find a solution. Coming here to hopefully find someone who can help out.

gsub works here:

local x = 6/17 --example. roughly 0.35294117647059
local noDecimalPointStr, _ = string.gsub(tostring(x), "%.", "")
print(noDecimalPointStr) -- 035294117647059

The "%." just means match any ‘.’ character and the "" means replace it with the empty string (aka delete the character).

1 Like
local hi = '323232.24294500'
local tb = string.split(hi,'.')
local num = tb[1]..tb[2]
local realNum = tonumber(num)
print(realNum)

Haven’t tested this but might be an alternative for gsub.

3 Likes
local number = 434244.12131232
local splitNumber = tostring(number):split(".")
local newNumber = number * tonumber(10^splitNumber[2]:len())
print(newNumber)

The 2 provided solutions work, I just decided to offer a slightly alternative route (which gets the length of the decimal) and multiplies the original number by 10 to the power of the length (number of digits) of the decimal part.