Open In App

Formatting Numbers and Strings in R Programming – format() Function

Last Updated : 23 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

In R programming, the format() function formats numbers, strings and dates to meet presentation needs. It gives formatting features to modify the display of numeric values, strings and date/time information. This function is applied to regulate the number of decimal places, alignment, scientific notation, etc. The format() function makes data display in the required format, making it easier to read and appropriate for reporting or data analysis.

Syntax:

format(x, digits, nsmall, scientific, width, justify = c(“left”, “right”, “center”, “none”)) 

Parameters: 

  • x: is the vector input.
  • digits: is the total number of digits displayed.
  • nsmall: is the minimum number of digits to the right of the decimal point.
  • scientific: is set to TRUE to display scientific notation.
  • width: indicates the minimum width to be displayed by padding blanks in the beginning.
  • justify: is the display of the string to left, right or center.

String formatting

The format() function can also center strings according to the width and alignment given.

Example: Aligning Strings

R
# Placing string in the left side
r_1 <- format("GFG", width = 8,  justify = "l") 

# in the center
r_2 <- format("GFG", width = 8,  justify = "c") 

# in the right
r_3 <- format("GFG", width = 8,  justify = "r") 

print(r_1) 
print(r_2) 
print(r_3)

Output
[1] "GFG     "
[1] "  GFG   "
[1] "     GFG"

Number formatting

The format() method can also be applied to control the display of numbers, such as specifying decimal places, scientific notation and rounding numbers.

Example 1: Rounding Numbers and Controlling Decimal Places

R
# Rounding numbers to a specified number of digits
r_1 <- format(12.3456789, digits=4)
r_2 <- format(12.3456789, digits=6)
print(r_1)
print(r_2)

#Setting the minimum number of digits to the right of the decimal point
r_3 <- format(12.3456789, nsmall=2)
r_4 <- format(12.3456789, nsmall=7)
print(r_3)
print(r_4)

Output
[1] "12.35"
[1] "12.3457"
[1] "12.34568"
[1] "12.3456789"

Example 2: Formatting Numbers with Scientific Notation

R
# Getting the number in the string form
r_1 <- format(1234)
r_2 <- format(12.3456789)
print(r_1)
print(r_2)

# Display numbers in scientific notation
r_3 <- format(12.3456789, scientific=TRUE)
r_4 <- format(12.3456789, scientific=FALSE)
print(r_3)
print(r_4)

Output
[1] "1234"
[1] "12.34568"
[1] "1.234568e+01"
[1] "12.34568"

Date and Time formatting

The format() function can also be utilized to format date-time objects in R. You can convert Date or POSIX objects to tailored string representations.

Example 1: Formatting Date-Time Objects

R
# Current date and time
x <- Sys.time()

d <- format(x, format = "%Y-%m-%d %H:%M:%S")
print(d)

Output
[1] "2025-04-23 09:16:49"

Example 2: Formatting Dates with Full Month Name

R
x <- as.Date("2023-06-27") 
d <- format(x, format = "%B %d, %Y")
print(d)

Output
[1] "June 27, 2023"

Date-Time Format Codes:

  • %Y: Year with century as a decimal number.
  • %B: Full month name.
  • %d: Day of the month as a decimal number.


Next Article

Similar Reads