1、计算数字位数,前位补零
-- 计算数字的位数
function DightNum(num)
if math.floor(num) ~= num or num < 0 then
return -1
elseif 0 == num then
return 1
else
local tmp_dight = 0
while num > 0 do
num = math.floor(num/10)
tmp_dight = tmp_dight + 1
end
return tmp_dight
end
end
-- 在整数数字前面加0,例:3 变成 "03"
-- local a = 3
-- local s = AddZeroFrontNum(2,a)
-- 那么s是"03"
function AddZeroFrontNum(dest_dight, num)
local num_dight = DightNum(num)
if -1 == num_dight then
return -1
elseif num_dight >= dest_dight then
return tostring(num)
else
local str_e = ""
for var =1, dest_dight - num_dight do
str_e = str_e .. "0"
end
return str_e .. tostring(num)
end
end
2、时间戳格式化
function ShortTime(difftime) --时间格式化 00:00:00
local hour = math.floor(difftime / (60 * 60) % 24);
local minute = math.floor(difftime / 60 % 60);
local sec = math.floor(difftime % 60)
local timeStr;
if hour < 10 then
hour = "0" .. hour
end
if minute < 10 then
minute = "0" .. minute
end
if sec < 10 then
sec = "0" .. sec
end
timeStr = hour .. ":" .. minute .. ":" .. sec--string.format("%s:%s:%s", hour, minute, sec);
return timeStr;
end
3、贝塞尔曲线(3点定一曲线)
function BezierLineSecondOrder() --2阶贝塞尔曲线(3点定一曲线)
this.points = {}
local p1 = Vector3.New(600,600,0) --起点
local p2 = Vector3.New(1000,300,0) --作用点
local p3 = Vector3.New(0,0,0) --终点
local t = 80 --插值(0~1)
for i = 1, 200 do --生成点阵
local v1 = Vector3.Lerp(p1,p2,i/t)
local v2 = Vector3.Lerp(p2,p3,i/t)
local find = Vector3.Lerp(v1,v2,i/t)
table.insert(this.points,find)
end
end
4、lua 值类型判断
function ValueType(v)
local typ = type(v)
if typ == "string" then
return VT.STR
elseif typ == "table" then
return VT.TAB
elseif typ == "boolean" then
return VT.BOOL
elseif typ == "number" then
local n, f = math.modf(v)
if f > 0 then
return VT.F32
elseif v >= -128 and v <= 127 then
return VT.S8
elseif v >= -32768 and v <= 32767 then
return VT.S16
elseif v >= -2147483648 and v <= 2147483647 then
return VT.S32
else
return VT.S64
end
end
end
5、io
function writefile(path, content, mode)
mode = mode or "w+b"
local file = io.open(path, mode)
if file then
if file:write(content) == nil then return false end
io.close(file)
return true
else
return false
end
end
function exists(path)
local file = io.open(path, "r")
if file then
io.close(file)
return true
end
return false
end
function readfile(path)
local file = io.open(path, "r")
if file then
local content = file:read("*a")
io.close(file)
return content
end
return nil
end
6、数字缩写
--格式化数字表现(超过万的过滤掉)
function SpeFormatNumberToStr(num)
if num < 100000 then
return tostring(num);
end
local myraid = math.floor(num / 10000);
local remain = math.floor(num % 10000);
if remain <= 0 then
remain = "";
end
if myraid >= 10000 then
local upVal = math.floor(myraid / 10000);
local remainMyraid = math.floor(myraid % 10000);
if remainMyraid <= 0 then
return string.format("%d亿", upVal);
end
return string.format("%d亿%d万", upVal, remainMyraid);
end
return string.format("%d万", myraid);
end
7、时间
--将时间转化为年月日
function FormatTimeToDate(time)
local CurYear = os.date("%Y", time);
local CurMon = os.date("%m", time);
local CurDay = os.date("%d", time);
return string.format("%d-%02d-%d", CurYear, CurMon, CurDay)
end
--时间转时间戳
function FormatTime(time, hour, min, sec, nextday)
local CurYear = os.date("%Y", time);
local CurMon = os.date("%m", time);
local CurDay = os.date("%d", time);
if nextday then
CurDay = CurDay + 1;
end
local CurSec = sec or os.date("%S", time);
return os.time({ year = CurYear, month = CurMon, day = CurDay, hour = hour, min = min, sec = CurSec})
end
8、tolua关于update函数使用,具体函数实现原理在LuaBeatEvent C#类中,关联event lua类中
UpdateBeat:Add(UpdateLoop)
UpdateBeat:Remove(UpdateLoop)