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
- Lua_各种工具方法
- javaBean的各种工具方法
- Lua_ uLua_C#调用Lua方法_019
- Lua_ uLua_Lua调用C#方法_020
- 各种工具
- lua_学习
- Java获取各种常用时间方法(工具类)
- java中各种类型转换成Byte[]的工具方法
- HttpClient的Post和Get各种方法的工具类
- Android开发工具类(各种方法调用 )
- 各种方法
- 各种方法
- java产生随机字符串的各种方法------工具类(包含过滤表情方法)
- 各种开发工具下载
- 各种在线工具
- 各种不错的工具
- 各种使用工具
- 各种在线工具集
- 服务提供者框架(Service Provider FrameWork)——jdbc连接
- Linux运行的过程
- Visual Studio中11个强大的调试技巧和方法
- 插入排序
- 数据结构(C语言版)课后习题1.4
- Lua_各种工具方法
- 春四月,碎碎念念
- com.microsoft.sqlserver.jdbc.SQLServerException: 不支持此服务器版本。目标服务器必须是 SQL Server 2000 或更高版本。
- eclipse+android source code
- 【周末福利】源码分享
- Amusing numbers
- iOS获取状态栏和导航栏尺寸(宽度和高度)
- MUD游戏编程 Winsock杂项函数
- 【好程序员笔记分享】——Foundation框架的基本对象