# Format Number of Decimal Places in R (2 Example Codes)

This tutorial explains how to **control the number of decimal places** within the R programming language. The table of contents looks as follows:

- Creating Example Data
- Control Decimal Places with the format R Function (Example 1)
- Control Decimal Places with the sprintf R Function (Example 2)
- Format Decimal Places in Global Options (Example 3)
- Further Resources for the Formatting of Numbers in R

Figure 1 shows an **overview of three different alternatives** for controlling decimal places in R.

**Figure 1: Format Decimal Places in R (Overview).**

However, in the following examples I will explain these alternatives in more detail. So let’s move directly to the examples…

## Create Example Data

Let’s first have a look at the default specifications of R, before we start with the examples. Consider the following numeric data object:

x <- 10.76567759879643247 # Create example data object |

x <- 10.76567759879643247 # Create example data object

If we print this number, the RStudio console returns the following:

x # Print example data # 10.76568 |

x # Print example data # 10.76568

As you can see: A number with five decimal places.

In the following examples, you will learn how to format these decimals in R. So let’s continue with the examples…

## Example 1: Apply format Function to Control Decimal Places of One Specific Number

If we want to format the decimal places of one specific number (or a vector of numbers), we can use the format function in combination with the round function and the specification nsmall. Consider the following R syntax:

format(round(x, 3), nsmall = 3) # Apply format function # "10.766" |

format(round(x, 3), nsmall = 3) # Apply format function # "10.766"

As you can see: The number of digits after the decimal point was reduced to three.

**Note:** The output of the format function is a character string. If we want to do calculations with this output, we need to convert it back to the numeric class first.

## Example 2: Format Decimal Places with sprintf Function

The sprintf R function also provides the possibility to control decimal places of a certain number or a numeric vector. Let’s have a look at the R syntax:

sprintf(x, fmt = '%#.3f') # Apply sprintf function # "10.766" |

sprintf(x, fmt = '%#.3f') # Apply sprintf function # "10.766"

The output is the same as before. Again, we created a character string with three decimal places.

## Example 3: Change Number of Digits in Global R Options

In this example, I’ll show you how to format the general options for printing digits in RStudio. Consider the following R syntax:

options(digits = 5) # Modify global options |

options(digits = 5) # Modify global options

Now let’s print our example data again:

x # Print example data # 10.766 |

x # Print example data # 10.766

As you can see, the console output is a number with only three decimal places (in contrast to the five decimal places that we had before).

With the previous R code (i.e. *options(digits = 5)*), we changed the number of maximum digits to 5. In our specific example, 2 digits before the decimal point and 3 digits after the decimal point. If there would be more digits before the decimal point, the number of digits after the decimal point would be reduced (e.g. 1000.8).

**Note:** The amount of digits has now been changed for our entire R session. If we would like to change the options back to the default specification, we would have to restart RStudio, or we would have to save the default specification in the forefront.

## Further Resources for Dealing with Numbers in R

If you want to learn more about the handling of numbers in R, I can recommend the following video of the YouTuber Anthony Damico. In the video, he explains different ways how to change options and global settings in R.

Besides the video, you might also want to read more tutorials on statisticsglobe.com. In this case, you could have a look at the following list of tutorials:

- sprintf R Function
- Disable Scientific Notation in R
- Convert Character to Numeric in R
- List of R Commands (+ Examples)
- The R Programming Language

This tutorial showed you how to format the number of decimal places in R. Do you have any feedback or further questions? Let me know in the comments 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