加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

lua 取一个数字的整数部分

发布时间:2020-12-14 22:22:22 所属栏目:大数据 来源:网络整理
导读:lua 在对两个整数进行除法操作时不会向C那样将结果转换成整数,而是自动转换成浮点数。( lua 没有数据类型之分)。如果要实现此功能需要取得结果中的整数部分。 math.ceil (x) Returns the smallest integer larger than or equal to x . --取一个数的整数

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读