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

查看:31
本文介绍了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天全站免登陆