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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM