如何在Lua中将小数向下取整?

9
print(math.floor(12.5928))

以上代码打印出12。如何使它打印出12.5?

4个回答

15

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


7
为了概括之前的答案,此函数将数字四舍五入到任意小数位数:
function round(number, decimals)
    local power = 10^decimals
    return math.floor(number * power) / power
end

然后,round(12.5928, 1)会得到12.5

5

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


-1

保留两位小数: print(math.floor((12.5928) * 100) / 100)


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接