# R Program to Format Decimal Places

In this example, we will learn to format decimal places using the sprintf() or the format() function in R.

## Example 1: Format Decimal Places in R Using sprintf()

``````num <- 1.34567

# use sprintf() to format decimal places of num
sprintf(num, fmt = '%#.4f')

Output: [1] "1.3457"``````

In the above example, we've used the `sprintf()` function to print the given floating-point number num to 4 decimal places. The 4 decimal places are given by the format `.4f`.

This means, the function prints only up to 4 places after the dot (decimal places), and f means to print the floating-point number.

## Example 2: Format Decimal Places in R Using format()

``````num <- 1.34567

# format decimal places using format()
format(num, digits = 5)

# Output: [1] "1.3457"``````

Here, we have used the `format()` function to format decimal places of `num`.

Since we have passed `digits = 5` inside `format()`, the number of digits to be returned along with the number before decimal point is 5.