Announcement Icon Online training class for Clinical R programming batch starts on Monday, 02Feb2026. Click here for details.

Round Numeric Values


Lesson Description
-
  • Sometimes, we want to work with the concept of "Round Numeric Values" in a clear, repeatable way.
  • This lesson walks through a simple example and shows the key steps.
  • We will see one approach on how we can do it in SAS and R.
data round_demo;
    input value;
    datalines;
3.1416 
2.718 
9.99 
1.5 
-4.75 
;
run;

data round_results;
    set round_demo;
    round_val = round(value);
    round_dec = round(value,
0.01);
    ceil_val = ceil(value);
    floor_val = floor(value);
run;
  • Demonstrates how to round numeric values in SAS.
  • `round(value)` rounds to the nearest integer.
  • `round(value,0.01)` rounds to the nearest second decimal.
  • `ceil(value)` returns the smallest integer greater than or equal to the value.
  • `floor(value)` returns the largest integer less than or equal to the value.
round_demo <- tribble( ~value, 
                     3.1416, 
                     2.718, 
                     9.99, 
                     1.5, 
                     -4.75 ) 

round_results <- round_demo %>% 
  mutate( 
    round_val = round(value), 
    round_dec = round(value,2), 
    ceil_val = ceiling(value), 
    floor_val = floor(value) 
    )
  • R uses `round()` to round values to the nearest integer.
  • `round(value,2)` to round values to the nearest second decimal.
  • `ceiling()` returns the smallest integer not less than the value.
  • `floor()` returns the largest integer not greater than the value.
round_demo <- data.frame(
  value = c(3.1416, 2.718, 9.99, 1.5, -4.75)
  , stringsAsFactors = FALSE
)

round_results <- round_demo

round_results$round_val <- round(round_results$value)

round_results$round_dec <- round(round_results$value, 2)

round_results$ceil_val <- ceiling(round_results$value)

round_results$floor_val <- floor(round_results$value)
  • round(x, 2) rounds to 2 decimals; ceiling/floor bound values.