I am coding a calculator and I was trying to print the division equation's answer, but instead it converted fractions to decimals. I am running Lua 5.2.4
print ("\n\tWhat math symbol will you use?")
ms = io.read()
-- Main function
function TYPCALC()
--If user typed certain math symbols
if (ms == "division") then
print ("\n\tWhat number will be divided to?")
local rn = io.read()
print ("\n\tWhat will be dividing?")
local ln = io.read()
--convert users answers in to strings
local cln = tonumber(ln)
local crn = tonumber(rn)
--do equasion
io.write (ln / rn)
end;
end ;
TYPCALC()
Lua does not have a fraction type. You'll have to calculate the numerator and denominator yourself. Then print it.
If you just print or write (number/number2) that expression will be evaluated first, resulting in a decimal number. The function will use a local copy of that number then.
will print
1/12Another remark:
If you want to convert a number to a string, you have to use tostring(), not tonumber()