转载自:http://blog.youkuaiyun.com/zhangxaochen/article/details/8095007
lua在对两个整数进行除法操作时不会向C那样将结果转换成整数,而是自动转换成浮点数。(lua没有数据类型之分)。如果要实现此功能需要取得结果中的整数部分。
math.ceil (x)
Returns the smallest integer larger than or equal to x
.
--取一个数的整数部分
function getIntPart(x)
if x <= 0 then
return math.ceil(x);
end
if math.ceil(x) == x then
x = math.ceil(x);
else
x = math.ceil(x) - 1;
end
return x;
end
print(getIntPart(12.345));
print(getIntPart(12.0));
print(getIntPart(12));
print(getIntPart(0));
print(getIntPart(-12));
print(getIntPart(-12.0));
print(getIntPart(-12.345));