如何在 Lua 中将 math.floor 转换为小数?

Gat*_*ion 5 lua floor

print(math.floor(12.5928))
Run Code Online (Sandbox Code Playgroud)

上面的代码打印 12。你怎么做才能让它打印 12.5?

Max*_*amy 9

print(math.floor(12.5928 * 10) / 10)


cyc*_*ist 6

为了概括之前的答案,此函数将数字四舍五入为任意位数:

function round(number, decimals)
    local power = 10^decimals
    return math.floor(number * power) / power
end
Run Code Online (Sandbox Code Playgroud)

然后,round(12.5928, 1)12.5.


Sri*_*m S 5

print(string.format("%.2f", 129.65686))