Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

How can I use string manipulation to tell if there is a decimal in a string?

Asked by 6 years ago

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!

0
One thing is I don't know if "Interger" exist in type. User#22219 20 — 6y
0
Of course not. I wrote Integer. Not Interger. Lol User#21908 42 — 6y
0
I found that using `Number:match('%d%d%d[.^]')` will do the trick: If it has 3 numbers and a period, then it'll return return the first 3 numbers and then a period, otherwise it'll return nil. TheeDeathCaster 2368 — 6y
0
I meant the decimal wherever it appears in the number User#21908 42 — 6y

2 answers

Log in to vote
2
Answered by
Rhythic 36
6 years ago

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.

0
So could I use this in an if statement to check and see if there is a period? User#21908 42 — 6y
0
Yes. It will return either true or false if used in a conditional statement. Rhythic 36 — 6y
Ad
Log in to vote
0
Answered by 6 years ago

You could use string.match or string.find or maybe string.gmatch (there should be a string pattern for decimals).

Answer this question