Lua千分位格式化数字

function number_format(num,deperator)
    local str1 =""
    local str = tostring(num)
    local strLen = string.len(str)
        
    if deperator == nil then
        deperator = ","
    end
    deperator = tostring(deperator)
        
    for i=1,strLen do
        str1 = string.char(string.byte(str,strLen+1 - i)) .. str1
        if math.mod(i,3) == 0 then
            --下一个数 还有
            if strLen - i ~= 0 then
                str1 = ","..str1
            end
        end
    end
    return str1
end
廖大的代码
function checknumber( value, base)
return tonumber(value, base) or 0
end

function formatnumberthousands( num)
local formatted = tostring( checknumber(num))
local k
while true do
formatted, k = string.gsub(formatted, "^(-?%d+)(%d%d%d)", '%1,%2')
if k == 0 then break end
end
return formatted
end



你可能感兴趣的:(Lua)