lua - When trying to print division, it converts fractions to decimals -
i coding calculator , trying print division equation's answer, instead converted fractions decimals. running lua 5.2.4
print ("\n\twhat math symbol use?") ms = io.read() -- main function function typcalc() --if user typed math symbols if (ms == "division") print ("\n\twhat number divided to?") local rn = io.read() print ("\n\twhat dividing?") local ln = io.read() --convert users answers in strings local cln = tonumber(ln) local crn = tonumber(rn) --do equasion io.write (ln / rn) end; end ; typcalc()
lua not have fraction type. you'll have calculate numerator , denominator yourself. print it.
if print or write (number/number2) expression evaluated first, resulting in decimal number. function use local copy of number then.
local denominator = 12 -- or calculated value in case local numerator = 1 print(numerator .. "/" .. denominator)
will print 1/12
another remark:
--convert users answers in strings local cln = tonumber(ln) local crn = tonumber(rn)
if want convert number string, have use tostring(), not tonumber()
Comments
Post a Comment