Округление до верхнего или нижнего значения в программировании на R - функция trunc ()

Опубликовано: 18 Февраля, 2022

trunc() function in R Language is used to return the largest integer that is smaller than or equal to x (i.e : rounds downs the nearest integer). trunc() function behaves as a ceiling function for negative number and floor function for positive number.

Syntax: trunc(x)

Parameter:
x: Numeric value to be rounded off

Example 1:

# R program to calculate trunc value 
    
# Using trunc() method 
answer1 <- trunc(3.4
answer2 <- trunc(-3.4
answer3 <- trunc(3.6
answer4 <- trunc(-3.6
  
print(answer1) 
print(answer2) 
print(answer3) 
print(answer4) 

Выход:

3
-3
3
-3

Example 2:

# R program to calculate trunc value 
    
# Using trunc() method 
answer1 <- trunc(c(1.5, 2.6, -3, -3.4)) 
  
print(answer1) 

Выход:

1 2 -3 -3