【发布时间】:2017-08-18 03:16:02
【问题描述】:
我正在编写一个计算器,我试图打印除法方程的答案,但它却将分数转换为小数。 我正在运行 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()
【问题讨论】:
-
没有内置分数类型。
-
尝试使用
string.format('%.2f", ln / rn)
标签: lua floating-point division fractions rational-numbers