So I have this situation where I don’t know the numbers in front of the string: “%”.
For example, I could get string “1220.44%”, string “20%”, string “0.4%”, etc.
How do I get the numbers in front of the ‘%’ sign?
Keep in mind that the “input” or data type I have access to is always a string.
1 Like
Sleazel
(Sleazel)
March 9, 2023, 9:07pm
#2
Use string.format
. As %
is a special character, to use it with this method you will need to type it twice:
print(string.format("%f%%",21.1142))
-- output:
-- 21.1142%
To restrict fraction to for example two places after dot, use:
--print(string.format("%f.2%%",21.1142))
EDIT:
-- Correction
print(string.format("%.2f%%",21.1142))
-- 21.11%
For whole numbers, use %d%%
instead.
1 Like
I don’t want the “%”. From your example, I only want the “21.1142” as a string.
I’m getting “21.1142%”, and I want “21.1142” but I won’t know how many numbers are in front of the “%”.
1 Like
yusatial
(yusa)
March 9, 2023, 10:07pm
#4
They wanted the string in front of ‘%’
This is going to work on all string types, but please note that if it’s “hello%%” or “1.131%%”, it’s going to return “hello” and “1.131” without the extra %.
string.match(percentage_string, "(.-)%%")
(Note: If you want the additional ‘%’, like ‘hello%%’ → ‘hello%’, you can use (.-%%)
as well.)
2 Likes
that’s just what I needed I will do checks! ty
1 Like
system
(system)
Closed
March 23, 2023, 10:10pm
#6
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.