formatC()

formatC(x, digits = 1, format = 'd', width = NULL, flag = '', mode = NULL, big.mark = '')
Returns: character · Updated March 13, 2026 · String Functions
formatting strings numbers formatC base

The formatC() function formats numbers using C-style format specifications. It provides fast formatting for numeric vectors.

Syntax

formatC(x, digits = 1, format = "d", width = NULL, flag = "", 
        mode = NULL, big.mark = "", big.interval = 3)

Parameters

ParameterTypeDefaultDescription
xnumericrequiredNumeric vector to format
digitsinteger1Number of digits after decimal
formatcharacter”d”Format code: “d” (integer), “f” (fixed), “e” (scientific), “g” (auto)
widthintegerNULLMinimum field width (pads with spaces)
flagcharacter""Flags like ”-” (left-align), “0” (zero-padding)
modecharacterNULL”real”, “integer”, or “complex”
big.markcharacter""Thousands separator

Examples

Integer format

# Basic integer
formatC(42, format = "d")
# [1] "42"

# Zero-pad integers
formatC(42, width = 5, format = "d")
# [1] "   42"

formatC(42, width = 5, flag = "0", format = "d")
# [1] "00042"

Fixed-point format

# Fixed decimals
formatC(3.14159, format = "f", digits = 2)
# [1] "3.14"

formatC(100, format = "f", digits = 2)
# [1] "100.00"

Scientific notation

# Scientific format
formatC(1234, format = "e")
# [1] "1.2e+03"

formatC(0.00001234, format = "e", digits = 2)
# [1] "1.2e-05"

Auto format

# Automatic format selection
formatC(c(1.5, 1500, 0.00015), format = "g")
# [1] "1.5"      "1500"     "0.00015"

Common Patterns

Table alignment

# Left-align and pad to width
formatC(42, width = 5, flag = "-", format = "d")
# [1] "42   "

# Right-align (default)
formatC(42, width = 5, format = "d")
# [1] "   42"

Thousands separator

# Add comma separator
formatC(1234567, big.mark = ",", format = "d")
# [1] "1,234,567"

formatC(1234567.89, big.mark = ",", format = "f", digits = 2)
# [1] "1,234,567.89"

Combining with data frames

df <- data.frame(
  id = 1:3,
  value = c(1234.56, 78.9, 0.12)
)

df$value <- formatC(df$value, format = "f", digits = 2)
df
#   id     value
# 1  1   1234.56
# 2  2     78.90
# 3  3      0.12

See Also