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 formattingThe 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. Comment More infoAdvertise with us Next Article Formatting Numbers and Strings in R Programming - format() Function K Kanchan_Ray Follow Improve Article Tags : R Language R String-Functions Similar Reads R Tutorial | Learn R Programming Language R is an interpreted programming language widely used for statistical computing, data analysis and visualization. R language is open-source with large community support. R provides structured approach to data manipulation, along with decent libraries and packages like Dplyr, Ggplot2, shiny, Janitor a 4 min read R Programming Language - Introduction R is a programming language and software environment that has become the first choice for statistical computing and data analysis. Developed in the early 1990s by Ross Ihaka and Robert Gentleman, R was built to simplify complex data manipulation and create clear, customizable visualizations. Over ti 4 min read R-Data Frames R Programming Language is an open-source programming language that is widely used as a statistical software and data analysis tool. Data Frames in R Language are generic data objects of R that are used to store tabular data. Data frames can also be interpreted as matrices where each column of a matr 6 min read Read contents of a CSV File in R Programming - read.csv() Function read.csv() function in R Language is used to read "comma separated value" files. It imports data in the form of a data frame. The read.csv() function also accepts a number of optional arguments that we can use to modify the import procedure. we can choose to treat the first row as column names, sele 3 min read R-Data Types Data types in R define the kind of values that variables can hold. Choosing the right data type helps optimize memory usage and computation. Unlike some languages, R does not require explicit data type declarations while variables can change their type dynamically during execution.R Programming lang 5 min read Data Visualization in R Data visualization is the technique used to deliver insights in data using visual cues such as graphs, charts, maps, and many others. This is useful as it helps in intuitive and easy understanding of the large quantities of data and thereby make better decisions regarding it.Data Visualization in R 6 min read apply(), lapply(), sapply(), and tapply() in R In this article, we will learn about the apply(), lapply(), sapply(), and tapply() functions in the R Programming Language. The apply() collection is a part of R essential package. This family of functions helps us to apply a certain function to a certain data frame, list, or vector and return the r 4 min read R-Matrices R-matrix is a two-dimensional arrangement of data in rows and columns. In a matrix, rows are the ones that run horizontally and columns are the ones that run vertically. In R programming, matrices are two-dimensional, homogeneous data structures. These are some examples of matrices:R - MatricesCreat 10 min read Functions in R Programming A function accepts input arguments and produces the output by executing valid R commands that are inside the function. Functions are useful when you want to perform a certain task multiple times. In R Programming Language when you are creating a function the function name and the file in which you a 8 min read R - Bar Charts Bar charts provide an easy method of representing categorical data in the form of bars. The length or height of each bar represents the value of the category it represents. In R, bar charts are created using the function barplot(), and it can be applied both for vertical and horizontal charts.Syntax 4 min read Like