在 R 编程中将值四舍五入到特定数字 - round()函数
R 语言提供了一个内置函数round()
,它舍入到给定的位数,如果没有为舍入提供位数,它会将数字舍入到最接近的整数。
Syntax: round(x, digits=n)
Parameter:
x: Number to be rounded off
digit: Specified digits
示例 1:
# R program to calculate round value
# Using round() method
answer1 <- round(2.356)
answer2 <- round(2.356, digits = 2)
answer3 <- round(2.5)
answer4 <- round(2.5, digits = 1)
print(answer1)
print(answer2)
print(answer3)
print(answer4)
输出:
2
2.36
2
2.5
示例 2:
# R program to calculate round value
# Using round() method
answer1 <- round(c(1.5, 2.6, -3, -3.4))
print(answer1)
输出:
2 3 -3 -3