In this question I will actually be asking about two different string manipulation problems I have. Number one:
How can I tell if there is a decimal in a number. Because when I am displaying money on my leaderboard as a string, I want it to display the first three digits along with the decimal. I tried using string.sub(mValue, 1, 3)
but that counted the decimal in the string. Please help me out with this issue. Second:
I have a admin script that I am writing for fun and wanted to shorten the length of an if statement. Here is the code:
plr.Chatted:Connect(function(msg) if string.lower(string.sub(msg, 1, 5)) == "speed" then if type(string.sub(msg, 7, 9)) == "Integer" or then -- here is my problem I want it to check if after a space after speed if it is a two digit or three digit number. However that would require a really long if statement is there an easier way to manipulate this? end end end)
Hope you guys can help. Thanks!
You could use the string.match
function to find decimal points in a string.
An example of this is using the %
character as a string pattern.
print(string.match("3.14", "%."))
As you can see I added a .
next to the %
character. Anything after the %
character will try to match it if there is one in the string! This will print the decimal point in the output. Meaning, there was a decimal point! It will return nil if there isn't a decimal point in the string.
You could use string.match or string.find or maybe string.gmatch (there should be a string pattern for decimals).