Lua:四舍五入然后截断 [英] Lua: Rounding numbers and then truncate

查看:411
本文介绍了Lua:四舍五入然后截断的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

哪种方法最有效地将数字四舍五入然后截断(四舍五入后删除小数位)?

Which is the best efficient way to round up a number and then truncate it (remove decimal places after rounding up)?

例如,如果十进制数大于0.5(即0.6、0.7等),我想四舍五入然后截断(情况1).否则,我想截断(案例2)

for example if decimal is above 0.5 (that is, 0.6, 0.7, and so on), I want to round up and then truncate (case 1). Otherwise, I would like to truncate (case 2)

for example:
232.98266601563 => after rounding and truncate = 233 (case 1)
232.49445450000 => after rounding and truncate = 232 (case 2)
232.50000000000 => after rounding and truncate = 232 (case 2)

推荐答案

Lua中没有内置math.round()函数,但是您可以执行以下操作: print(math.floor(a+0.5)).

There is no build-in math.round() function in Lua, but you can do the following: print(math.floor(a+0.5)).

这篇关于Lua:四舍五入然后截断的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆