微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

lua 取一个数字的整数部分

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));

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐