Converting strings with fractions in them to numbers

Let’s say I have a fraction within a string: “1/2”.

If I use the tonumber() on that string, it should return 0.5, but instead it returns nil because of the division sign.
Is there any other way I could convert string with fractions in them into numbers?

local fraction = "1/2"
local number = tonumber(fraction)
print(number) -- should return 0.5, actually returns nil

Maybe you could get the first and last letters of the string, convert them, and divide them?

problem with that is, if you get strings like “56/100”, getting the first and last letters won’t work

Ok, first let’s check for a division symbol and get back and next letters and check if they are numbers if so tostring back and next letters to numbers and divide them.

Keep getting the back string and check if it’s a number.

local values = string.split(myfraction, "/")
print(values[1], values[2])
local dec = tonumber(values[1]) / tonumber(values[2])
1 Like

1000 / 1

check for “/“ and start from there, keep going back and coward until you reach the end of the string or the letter you are on isn’t a number.