Open In App

Rounding off to the ceiling or floor value in R Programming – trunc() Function

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) 

Output:

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) 

Output:

1  2 -3 -3
Article Tags :