How to Keep Trailing Zeros After Rounding in R (Example Code)

In this post you’ll learn how to retain zeros at the end of a number after rounding in R programming.

Introducing Example Data

my_data <- 5.55                       # Example data
my_data
# [1] 5.55
round(my_data, 5)                     # Rounding removes zeros
# [1] 5.55

Example: How to Keep Trailing Zeros Using the sprintf() Function

sprintf("%.5f", round(my_data, 5))    # Applying sprintf
# [1] "5.55000"

Leave a Reply

Your email address will not be published.

Fill out this field
Fill out this field
Please enter a valid email address.
You need to agree with the terms to proceed

Menu
Top