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 Convert a String into Date Format in R Programming - as.Date() Function as.Date() function in R Language is used to convert a string into date format. Syntax: as.Date(x, format) Parameters: x: string variable format: Format in which string is declared(%m/%d/%y) Example 1: Python3 1== # R program to convert string into date # Creating a string vector dates <- c(" 1 min read Print a Formatted string in R Programming - sprintf() Function The sprintf() function is used to create formatted strings in R. These formatted strings enable us to insert variables and values into a string while controlling their appearance and formatting. The sprintf() function uses a user-defined format to return a formatted string by inserting the correspon 2 min read Convert an Object to a String in R Programming - toString() Function toString() function in R Language is used to convert an object into a single character string. Syntax: toString(x, width) Parameters: x: Object width: maximum string width Example 1: Python3 1== # R program to convert an object to string # Creating a vector x <- c("Geeks", "for 1 min read Convert a Vector into Factor in R Programming - as.factor() Function as.factor() function in R Programming Language is used to convert the passed object(usually Vector) into a Factor. Syntax: as.factor(object) Parameters: Object: Vector to be convertedas.factor() Function in R ExampleExample 1: Convert a Factor in RR # Creating a vector x<-c("female", "male", "ma 1 min read Convert Factor to Numeric and Numeric to Factor in R Programming Factors are data structures that are implemented to categorize the data or represent categorical data and store it on multiple levels. They can be stored as integers with a corresponding label to every unique integer. Though factors may look similar to character vectors, they are integers and care m 5 min read String Concatenation in R Programming String concatenation is a way of appending two or more strings into a single string whether it is character by character or using some special character end to end. There are many ways to perform string concatenation. Example: Input: str1 = 'Geeks' str2 = 'for' str3 = 'Geeks' Output: 'GeeksforGeeks 3 min read Convert a Numeric Object to Character in R Programming - as.character() Function as.character() function in R Language is used to convert a numeric object to character object. Syntax: as.character(x) Parameters: x: Numeric Object Example 1: Python3 1== # R program to convert a numeric object # to character object # Calling as.character() function as.character(1) as.character(2 + 1 min read Create Repetitions of a String in R Programming - strrep() Function strrep() Function in R Language is used to create specified number of repetitions of a specified string. Syntax: strrep(string, n) Parameter: string: specified string n: number of repetitions Example 1: Python3 1== # R Program to illustrate # the use of strrep function # String to be repeated x < 1 min read Taking Input from User in R Programming Developers often have a need to interact with users, either to get data or to provide some sort of result. Most programs today use a dialog box as a way of asking the user to provide some type of input. Like other programming languages in R it's also possible to take input from the user. For doing s 7 min read Convert an Integer to UTF8 in R Programming - intToUtf8() Function intToUtf8() function in R Language is used to convert an integer into UTF8 value. Syntax: intToUtf8(x, multiple) Parameters: x: Integer or integer vector multiple: Boolean value to convert into single or multiple strings Example 1: Python3 1== # R program to convert an integer to UTF8 # Calling the 1 min read Like