# Keep Trailing Zeros in R (2 Examples)

In this article, Iâ€™ll explain how to **keep zeros at the end of a number** in the R programming language.

The page looks as follows:

Letâ€™s get started.

## Example Data

Weâ€™ll use the following data as basement for this R programming tutorial.

x <- 1.1001 # Create example data x # Print example data # [1] 1.1001

Have a look at the previous output of the RStudio console. It shows that the exemplifying data is a single numerical data object.

Letâ€™s assume that we want to round this number to three digits after the decimal point. Then, we might use the round function as shown below:

round(x, 3) # Round example data # [1] 1.1

As you can see, the previous output of the RStudio console shows only one digit after the decimal point. The reason for this is that the remaining two digits are equal to zero.

In the following examples, Iâ€™ll explain how to keep those trailing zeros in your data output.

## Example 1: Keep Trailing Zeros Using sprintf() Function

This example explains how to round a number and make it show zeros at the end using the sprintf function.

Have a look at the following R syntax:

x_new1 <- sprintf("%.3f", round(x, 3)) # Keep trailing zeros x_new1 # Print updated data with zeros # [1] "1.100"

As you can see, our number has been returned with two trailing zeros. Note that this value is formatted as a character string and not as numerical data anymore.

## Example 2: Add Trailing Zeros Using paste0() Function

In this example, Iâ€™ll illustrate how to use the paste0 function to add trailing zeros to a data object.

Consider the R code below:

x_new2 <- paste0(round(x, 3), "00") # Add trailing zeros x_new2 # Print updated data with zeros # [1] "1.100"

As you can see, the previous output is the same as in Example 1.

In my opinion, the R code of Example 1 is preferable in most cases, since you always would have to specify the exact number of zeros you want to add within the paste0 function. However, this strongly depends on your specific situation and your personal preferences!

## Video & Further Resources

Do you want to learn more about the keeping of zeros at the end after rounding a number? Then I can recommend watching the following video on my YouTube channel. I show the R code of the present tutorial in the video.

In addition, you could read the other articles on this website. I have published several articles already:

- Add Leading Zeros in R
- Remove Leading and Trailing Zeros
- Create a Vector of Zeros in R
- Create a Vector of Zero Length
- R Programming Examples

You have learned in this article how to **retain zeros at the end of a number after rounding** in the R programming language. Note that we could apply the same code to a vector or data frame column as well. If you have any additional questions, tell me about it in the comments.