Lua_各种工具方法

来源:互联网 发布:挡土墙计算软件 编辑:程序博客网 时间:2024/05/03 02:23
Utils = {}local curTime = 1429283677local curNum = 10.5local curTab = {1, 2, 3, 4}local curStr = "Hello"local curStrTab = {'a', 'b', 'c', 'd'}--将秒转成"年/月/日"function Utils.remainTimeToDates(times)    local tab = os.date("*t", times);local dates = tab.year.."/"..tab.month.."/"..tab.dayreturn datesend--print(Utils.remainTimeToDates(curTime))--将秒转成"年.月.日 小时:分钟:秒"function Utils.remainTimeToDateTime(times)    local tab = os.date("*t", times)local dates = string.format("%d.%d.%d %02d:%02d:%02d",tab.year,tab.month,tab.day,tab.hour,tab.min,tab.sec)return datesend--print(Utils.remainTimeToDateTime(curTime))--将时间格式化为hh:mm:ss,主要用于倒计时function Utils.remainTimeToStringHHMMSS(remainTime)    if not remainTime or remainTime <= 0 then        return "00:00:00"    end    local h = math.floor(remainTime/1000/60/60)    local m = math.floor(remainTime/1000/60)%60    local s = math.floor(remainTime/1000)%60    return string.format("%02d:%02d:%02d",h,m,s)end--print(Utils.remainTimeToStringHHMMSS(curTime))--将时间格式化为mm:ss,主要用于倒计时function Utils.remainTimeToStringMMSS(remainTime)    if not remainTime or remainTime <= 0 then        return "00:00"    end    local h = math.floor(remainTime/1000/60/60)    local m = math.floor(remainTime/1000/60)%60    local s = math.floor(remainTime/1000)%60    return string.format("%02d:%02d",m,s)end--print(Utils.remainTimeToStringMMSS(curTime))--将浮点数转化为整数function Utils.floatToInt(n)    local s = tostring(n)    local i, j = s:find('%.')    if i then        return tonumber(s:sub(1, i-1))    else        return n    endend--print(Utils.floatToInt(curNum))--字符串转成tablefunction Utils.stringToArray(str)local i = 1local tab = {}while i <= #str  dolocal c = str:sub(i, i)local ord = c:byte()if ord > 128 thentable.insert(tab, str:sub(i, i + 2))i = i + 3elsetable.insert(tab, c)i = i + 1endendreturn tabendfor k, v in ipairs(Utils.stringToArray(curStr)) do--print("k:"..k.."   v:"..v)end--table转成字符串function Utils.tableToString(tab,text)local str = ""if type(text) == 'string' thenstr = "['"..text.."']".."={"elsestr = "{"end    if tab then        for k,v in pairs(tab) do    if type(v) == 'table' then    str = str..self.tableToString(v, k)..","    else    if type(k) == 'string' then        str = str.."['"..k.."']".."="    end    if type(v) == 'string' then            str = str.."'"..v.."',"    elseif type(v) == 'boolean' then    str = str..(v and 'true' or 'false')..","    else    str = str..v..","    end    end        end     else        str= str.."null,"     endstr = str.."}"    return strend--print(Utils.tableToString(curTab,"rrr"))--字符数组转换成整形数组function Utils.strArrToIntArr(strArr)local intArr = {}for k,v in ipairs(strArr) dointArr[k] = string.byte(v)endreturn intArrendfor k, v in pairs(Utils.strArrToIntArr(curStrTab)) do--print("k:"..k.."   v:"..v)end--四舍五入function Utils.round(num)    if math.ceil(num) - num <= 0.5 then        return math.ceil(num)    else        return math.floor(num)    end    end--print(Utils.round(curNum))--计算两点的距离function Utils.calcPointsDistance(p1, p2)    return math.sqrt(math.pow((p1.x - p2.x), 2) + math.pow((p1.y - p2.y), 2))end--print(Utils.calcPointsDistance({x=1, y=2}, {x=3, y=4}))--table深度拷贝function Utils.DeepCopy(object)    local backup_table = {}    local function _copy(object)        if type(object) ~= "table" then            return object        elseif backup_table[object] then            return backup_table[object]        end        local new_table = {}        backup_table[object] = new_table        for index, value in pairs(object) do            new_table[_copy(index)] = _copy(value)        end        return setmetatable(new_table, getmetatable(object))    end    return _copy(object)endfor k, v in pairs(Utils.DeepCopy(curTab)) do--print("k:"..k.."   v:"..v)end

0 0