# Rounding in R (4 Examples) | round, ceiling, floor, trunc & signif Functions

This tutorial explains how to **round numbers in R** with the functions round(), ceiling(), floor(), trunc(), and signif().

The article shows in **four examples** how the different rounding functions are used in the R programming language.

Sound good? Let’s dive right in…

## Example 1: Comparison of round, ceiling, floor, trunc & signif

The R programming language provides many different rounding functions, which are all slightly different.

Figure 1 illustrates based on four different input values (i.e. 1.1, 1.9, – 1.1, and – 1.9) how the output of the different functions looks like.

Values in red cells were rounded down to the next lower number and values in green cells were rounded up to the next higher numbers.

**Figure 1: Comparison of the round, ceiling, floor, trunc & signif R Functions.**

Definition of **round R function:** The round function rounds a numeric input to a specified number of decimal places.

Definition of **ceiling R function:** The ceiling function rounds a numeric input up to the next higher integer.

Definition of **floor R function:** The floor function rounds a numeric input down to the next lower integer.

Definition of **trunc R function:** The trunc function truncates (i.e. cuts off) the decimal places of a numeric input.

Definition of **signif R function:** The signif function rounds a numeric input to a specified number of digits.

In the following, you can find the R code that computes the values of Figure 1. First, we need to create numeric data objects that we can use in the following rounding examples:

x1 <- 1.1 # Create example values x2 <- 1.9 x3 <- - 1.1 x4 <- - 1.9 |

x1 <- 1.1 # Create example values x2 <- 1.9 x3 <- - 1.1 x4 <- - 1.9

Now, we can apply each of the five functions to these four example values. First, we apply the round function, …

round(x1) # Apply round function round(x2) round(x3) round(x4) |

round(x1) # Apply round function round(x2) round(x3) round(x4)

…then we apply the ceiling function, …

ceiling(x1) # Apply ceiling function ceiling(x2) ceiling(x3) ceiling(x4) |

ceiling(x1) # Apply ceiling function ceiling(x2) ceiling(x3) ceiling(x4)

…the floor function, …

floor(x1) # Apply floor function floor(x2) floor(x3) floor(x4) |

floor(x1) # Apply floor function floor(x2) floor(x3) floor(x4)

…the trunc function, …

trunc(x1) # Apply trunc function trunc(x2) trunc(x3) trunc(x4) |

trunc(x1) # Apply trunc function trunc(x2) trunc(x3) trunc(x4)

…and the signif function:

signif(x1, digits = 1) # Apply signif function signif(x2, digits = 1) signif(x3, digits = 1) signif(x4, digits = 1) |

signif(x1, digits = 1) # Apply signif function signif(x2, digits = 1) signif(x3, digits = 1) signif(x4, digits = 1)

As you can see, the R syntax of the five functions is very similar. However, there are plenty of situations where we need to adjust the specifications of the rounding functions. So keep on reading…

## Example 2: Round to Certain Amount of Digits

In the previous example, we rounded our data to zero decimal places. However, the round and signif functions can be used to round to a certain amount of decimal places.

Consider the following example number:

x5 <- 1.23456789 # Create numeric with many digits |

x5 <- 1.23456789 # Create numeric with many digits

We can use the round function to round this number to, for instance, **five decimal places**:

round(x5, digits = 5) # Round to five decimal places # 1.23457 |

round(x5, digits = 5) # Round to five decimal places # 1.23457

Or we can use the signif command to round our example number to **five digits**:

signif(x5, digits = 5) # Round to five digits # 1.2346 |

signif(x5, digits = 5) # Round to five digits # 1.2346

Note: The **difference between round and signif** is that round allows to specify the number of decimal places and signif allows to specify the number of digits (i.e. places before AND after the decimal point).

## Example 3: Round to Nearest 10

The round function can be used to round to the next 10 (i.e. 10, 20, 30, 40…). Consider the following numeric value:

x10 <- 77 # Create number with two digits |

x10 <- 77 # Create number with two digits

We can round this numeric value (i.e. the number 77) to the closest 10 by specifying a negative integer to the digits option of round:

round(x10, digits = - 1) # Round to nearest 10 # 80 |

round(x10, digits = - 1) # Round to nearest 10 # 80

Note that this principle can also be applied in order to round to the next 100, 1000, 10000, and so on. Simply increase the negative specification of digits.

## Example 4: Round .5 Up to Next Higher Value

The round function rounds .5 (e.g. 0.5, 7.5, 10.5 etc.) to the next even number. This can be illustrated with the following example.

Rounding 1.5 to zero decimal places results in the value 2 (i.e. .5 is rounded upwards):

round(1.5) # Round up to next even value # 2 |

round(1.5) # Round up to next even value # 2

However, rounding 2.5 results in the value 2 as well (i.e. .5 is rounded downwards):

round(2.5) # Round down to next even value # 2 |

round(2.5) # Round down to next even value # 2

If we always want to round .5 up, we need to create our own function. The following user-defined function always rounds .5 upwards (note: the function is based on a code that I found here):

round2 <- function(x, digits = 0) { # Function to always round 0.5 up posneg <- sign(x) z <- abs(x) * 10^digits z <- z + 0.5 z <- trunc(z) z <- z / 10^digits z * posneg } |

round2 <- function(x, digits = 0) { # Function to always round 0.5 up posneg <- sign(x) z <- abs(x) * 10^digits z <- z + 0.5 z <- trunc(z) z <- z / 10^digits z * posneg }

Let’s apply our new function to 1.5 and 2.5 (as we did before with the original round function):

The value 1.5 is rounded to 2…

round2(1.5) # Apply new rounding function # 2 |

round2(1.5) # Apply new rounding function # 2

…and the value 2.5 is rounded to the odd number 3:

round2(2.5) # Apply new rounding function # 3 |

round2(2.5) # Apply new rounding function # 3

## Further Resources for the Formatting of Numeric Data in R

If you need further examples for the different rounding functions, I can recommend the following video of the R-programming Library YouTube channel:

In addition, you could have a look at some of the other R programming tutorials on this website. I’m publishing new R tutorials on a regular basis:

This article explained how to round numeric data in R. However, if you have any further questions or comments, let me know in the comments section below.

### Subscribe to my free statistics newsletter:

### R Tutorials

abs Function in R

all & any R Functions

Set Aspect Ratio of Plot

attach & detach R Functions

attr, attributes & structure in R

cbind R Command

Change ggplot2 Legend Title

Character to Numeric in R

Check if Object is Defined

col & row sums, means & medians

Complete Cases in R

Concatenate Vector of Strings

Convert Date to Weekday

cumsum R Function

Data Frame Column to Numeric

diff Command in R

difftime R Function

dim Function in R

dir R Function

Disable Scientific Notation

Draw Segments in R

droplevels R Example

Evaluate an Expression

Extract Characters from String

Factor to Numeric in R

Format Decimal Places

get, get0 & mget in R

is.na R Function

is.null Function in R

jitter R Function

Join Data with dplyr Package

length Function in R

lowess R Smoothing Function

max and min Functions in R

NA Omit in R

nchar R Function

ncol Function in R

nrow Function in R

outer Function in R

pairs & ggpairs Plot

parse, deparse & R expression

paste & paste0 Functions in R

pmax and pmin R Functions

polygon Plots in R

pretty R Function

R Find Missing Values

R Functions List (+ Examples)

R NA – Values

R Replace NA with 0

rbind & rbind.fill in R

Read Excel Files in R

readLines, n.readLines & readline

Remove Element from List

Remove Legend in ggplot2

Rename Column Name in R

Replace Last Comma of String

rev R Command

Round Numeric Data in R

Save & Load RData Workspace

scan R Function

setdiff R Function

setNames vs. setnames in R

sink Command in R

Sort, Order & Rank Data in R

sprintf Function in R

Square Root in R

str_c Function of stringr Package

str_sub Function of stringr Package

strptime & strftime Functions

substr & substring R Commands

sweep R Function

Transform Data Frames

union Function in R

unlist in R

weekdays, months, quarters & julian in R

with & within R Functions

Write Excel File in R