--Lua-四舍五入(常用)
function Round(num, i)
local mult = 10^(i or 0)
local mult10 = mult * 10
return math.floor((num * mult10 + 5)/10)/ mult
end
--Lua-四舍五入(奇进偶舍)
function Round2(num, i)
local tmp = math.abs(num)*(10^(i+1))
local cal = math.abs(num)*(10^(i+1))/(10^(i+1))
local result = 0
if(math.floor(tmp)-math.floor(tmp/10)*10==5) then
if(tmp-math.floor(tmp)==0) then
local numInt1,numInt2 = math.modf(math.floor(tmp/10))
if(numInt1 % 2 == 0) then
result = math.floor(tmp/10)/(10^i)
end
end
end
if(result==0) then
result = math.floor((cal * ((10^(i or 0)) * 10) + 5)/10)/ (10^(i or 0))
end
return ( num>0 and result ) or (0-result)
end
--相乘,判断了是否有null值
function Multiply(num1,num2)
if(num1==nil) then
return 0
end
if(num2==nil) then
return 0
end
local temp=tostring(num1*num2)
temp=tonumber(temp)
return temp
end
--相除
function Divide(denominator,numerator)
if(numerator==nil) then
return 0
end
if(denominator==nil) then
return 0
end
if(numerator==0) then
return 0
end
return denominator/numerator
end
--取整
function Ceil(num)
if(num==nil) then
return 0
end
if (num <= 0) then
return math.ceil(num)
end
if (math.ceil(num) == num) then
return math.ceil(num)
else
return math.ceil(num) - 1
end
end
--取整
function Ceil2(num)
if(num==nil) then
return 0
end
local t1,t2 = math.modf(num)
return t1
end
Lua几种计算公式
最新推荐文章于 2024-11-07 22:38:40 发布
