Let’s say I have a number “1.200”, how do I convert that into “1200”?
1.200 and 1200 are vastly different numbers, but you can multiply 1.2 by 1000 to get 1200 if thats what you’re asking?
nvm, your actually asking how to remove a decimal, I suppose you could tostring() the value, then locate the decimal, remove it from string, then use tonumber()
although you may need to use string.format() instead of tostring() because of this:
But the amount of numbers isn’t constant, so like it can be 12.000 120.000, I won’t know that.
local number = 1.2
local rounded = math.round(1.2)
print(rounded) --1
You just use math.round()
to round a decimal to its nearest integer.
I don’t wanna change the number
You’re just trying to remove the decimal?
local numberWithDecimal = "1.200"
local numberWithoutDecimal = numberWithDecimal:gsub("%.", "")
print(numberWithoutDecimal) --1200
local decimalNumber = 1.23456789
local stringDecimalNumber = tostring(decimalNumber)
local stringNumber = stringDecimalNumber:gsub("%.", "")
local number = tonumber(stringNumber)
print(number) --123456789
You can multiply the decimal, for example
local times = 100
local number = 1.200
number = number * times
print(number)
1.200 = 1.2, so it would turn into 12 instead of 1200.
Just use string.split(.) and do splited[1]…splited[2]
Or just search the forum for how to round numbers. There are a lot of posts about this topic already.
You could use math.round(number * 100) / 100
so it basically multiplies it by 100, rounds it, then divides it by 100 so it is the number rounded to 2 decimal places.
I already said this, please read the full discussion before answering: I don’t want the number to be changed, I just want to remove the decimal
So then mark @Forummer’s answer in the 6th post as the Solution.
And maybe you should have said you wanted to keep the digits and remove the decimal instead of the way you worded it.
Saying you want to change 1.200 to 1200 is pretty misleading.