math.round()

类型 函数
math.*
返回值 数字
修订 版本 2024.3703
关键词 round
另请参阅 math.ceil()
math.floor()

概述

将数字四舍五入到最接近的整数,遵循与 JavaScript 版本相同的规则,即如果数字的小数部分是 0.5 或更大,则该参数舍入到下一个更高的整数。如果数字的小数部分小于 0.5,则该参数舍入到下一个较低的整数。

语法

math.round( x )
x (必填)

数字 要舍入的数字。

示例

print( math.round( 0.1 ) )   -- Output: 0
print( math.round( 0.5 ) )   -- Output: 1
print( math.round( 8.9 ) )   -- Output: 9 
print( math.round( -0.1 ) )  -- Output: 0
print( math.round( -0.5 ) )  -- Output: 0
print( math.round( -8.9 ) )  -- Output: -9