R语言中的四舍五入值 - round()函数
R 语言中的round()函数用于将值四舍五入为特定的小数位数。
Syntax: round(x, digits)
Parameters:
x: Value to be round off
digits: Number of digits to which value has to be round off
示例 1:对值进行四舍五入
Python3
# R program to illustrate
# round function
# Create example values
x1 <- 1.2
x2 <- 1.8
x3 <- - 1.3
x4 <- 1.7
# Apply round function
round(x1)
round(x2)
round(x3)
round(x4)
print(x1, x2, x3, x4)
Python3
# R program to illustrate
# round to a certain digit
# Create numeric with many digits
x2 <- 3.14734343243
# Round to three decimal places
round(x2, digits = 3)
print(x2)
输出 :
1
2
-1
2
在上面的代码中,我们使用函数round()用 4 个数据 x1、x2、x3 和 x4 对值进行了四舍五入,并打印了新值。
示例 2:四舍五入到某些数字
Python3
# R program to illustrate
# round to a certain digit
# Create numeric with many digits
x2 <- 3.14734343243
# Round to three decimal places
round(x2, digits = 3)
print(x2)
输出:
3.147
在上面的代码中,数值向量的值被四舍五入到 3 位。