rounded()
方法将指定值四舍五入到最近的整数或长整数,并返回该值。也就是说,3.87 四舍五入为 4,而 3.24 四舍五入为 3。
示例
// round the value 3.87
var result = 3.87.rounded()
print(result)
// Output: 4.0
rounded() 语法
rounded()
方法的语法是:
num.rounded()
这里,num
是一个数字。
rounded() 参数
rounded()
方法不接受任何参数。
rounded() 返回值
rounded()
方法返回最接近的整数值。
示例:Swift Double rounded()
// round the value 1.878
var result1 = 1.878.rounded()
print(result1) // 2.0
// round the value 1.5
var result2 = 1.5.rounded()
print(result2) // 2.0
// round the value 1.34
var result3 = 1.34.rounded()
print(result3) // 1.0
输出
2.0 2.0 1.0
在此,使用rounded()
方法将值四舍五入到最接近的整数。