Lua保留指定小数位数

默认会四舍五入

  • 比如:%0.2f 会四舍五入后,保留小数点后2位
print(string.format("%.1f",0.26)) 
---会输出0.3,而不是0.2

Lua保留一位小数

--- nNum 源数字
--- n 小数位数
function Tool. GetPreciseDecimal(nNum, n)
    if type(nNum) ~= "number" then
        return nNum;
    end
    n = n or 0;
    n = math.floor(n)
    if n < 0 then
        n = 0;
    end
    local nDecimal = 10 ^ n
    local nTemp = math.floor(nNum * nDecimal);
    local nRet = nTemp / nDecimal;
    return nRet;
end

参考:https://www.cnblogs.com/pk-run/p/4444582.html

posted @ 2018-05-11 20:37  赵青青  阅读(21107)  评论(0编辑  收藏  举报