I'm reading values from a file. These values could be strings or numbers - I don't know which beforehand - and I'd like to determine the data type after reading. My initial thought was to read in values as a string, cast as a number and then back to a string again and check if this is equal to the original value. For example,
a = "10.5"
if tostring(tonumber(a)) == a then
print("It's a number!")
print(tostring(tonumber(a)))
end
-- It's a number!
-- 10.5
This works fine for the given example, but if I change a = "10.50" then I run into problems because tostring(tonumber("10.50")) becomes 10.5 rather than 10.50.
I could do some cleaning and remove trailing zeros after reading from file, which is fine and totally doable, and then use the above code, but I wondered if there is an idiomatic way in Lua of checking type of a value that is read in from file?