formatC()
formatC(x, digits = 1, format = 'd', width = NULL, flag = '', mode = NULL, big.mark = '') 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
| Parameter | Type | Default | Description |
|---|---|---|---|
x | numeric | required | Numeric vector to format |
digits | integer | 1 | Number of digits after decimal |
format | character | ”d” | Format code: “d” (integer), “f” (fixed), “e” (scientific), “g” (auto) |
width | integer | NULL | Minimum field width (pads with spaces) |
flag | character | "" | Flags like ”-” (left-align), “0” (zero-padding) |
mode | character | NULL | ”real”, “integer”, or “complex” |
big.mark | character | "" | 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
The "f" format code locks numbers to a fixed number of decimal places, making it the right choice for financial data and measurements where precision must be explicit. Setting digits = 2 with format = "f" guarantees every value shows exactly two decimal places, even whole numbers like 100 become "100.00".
# Fixed decimals
formatC(3.14159, format = "f", digits = 2)
# [1] "3.14"
formatC(100, format = "f", digits = 2)
# [1] "100.00"
Scientific notation
Use format = "e" when numbers span many orders of magnitude and fixed-point notation would produce unreadable strings full of leading or trailing zeros. The exponent is always shown with a sign and at least two digits, so 1.2e+03 means 1200 and 1.2e-05 means 0.000012. This format is standard in engineering and physical science reporting.
# Scientific format
formatC(1234, format = "e")
# [1] "1.2e+03"
formatC(0.00001234, format = "e", digits = 2)
# [1] "1.2e-05"
Auto format
The "g" format lets R pick the shorter of fixed-point or scientific notation for each value independently. This is useful when a single vector mixes everyday numbers with extreme values — 1500 stays readable as "1500" while 0.00015 becomes "0.00015" rather than scientific notation. However, it means values in the same column may have inconsistent formatting, which is undesirable for tabular reports.
# Automatic format selection
formatC(c(1.5, 1500, 0.00015), format = "g")
# [1] "1.5" "1500" "0.00015"
Common patterns
The examples above demonstrate individual format codes in isolation. Real workflows combine formatC() with data frames to produce aligned, readable output. The width and flag arguments are especially useful when generating fixed-width reports or log files where every value must occupy the same number of characters.
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
Adding big.mark = "," makes large numbers scannable — 1234567 becomes "1,234,567". This works with any format code: combine with format = "f" and digits = 2 to get "1,234,567.89", the standard accounting representation. The separator character defaults to nothing, so you must set it explicitly whenever displaying values above a thousand.
# 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
Assigning formatC() output to a data frame column transforms the raw numbers into consistently formatted strings ready for display. The function preserves vector length, so df$value <- formatC(df$value, format = "f", digits = 2) replaces a numeric column with a character column in one step. After this assignment, the column prints aligned but can no longer be used in arithmetic — plan your formatting as the final step before output.
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
formatC() format codes
The format argument controls the number representation:
"f", fixed-point notation (e.g.,1234.56). Best for financial figures and measurements."e", scientific notation with a lowercasee(e.g.,1.23e+03). Use for very large or very small values."E", scientific notation with uppercaseE."g", whichever of"f"or"e"is shorter (R’s default for printing). Use when the scale varies widely."s", string formatting. Use with character data to left- or right-align and pad to a fixed width."d", integer formatting. Use with whole numbers; it drops any decimal part.
The flag argument accepts characters like "-" for left-alignment, "0" for zero-padding, and "+" for explicit sign. Combining width, digits, format, and flag gives precise control over column-style output, a common need when generating fixed-width reports or export files.
formatC() vs format() for column alignment
formatC() with a fixed width argument produces strings of exactly that character width, padding with spaces on the left (right-aligning numbers) or on the right (left-aligning with flag = "-"). This makes it the right tool for generating fixed-width text output where all values in a column must occupy the same number of characters.
format() does similar alignment but uses R-style arguments rather than C format codes. For most R users already familiar with base R, format() is easier to remember. For developers who know C printf conventions, formatC() is more intuitive.
When used on a data frame column before printing or writing to a text file, formatC(df$value, format = "f", digits = 2, width = 10) ensures every value is exactly 10 characters wide with 2 decimal places, a common requirement for tabular reports, fixed-width exports, and formatted summaries.
formatC() provides C-library-style formatting. The format argument accepts "f" (fixed), "e" (scientific), "g" (shorter of f/e), "d" (integer), and "s" (string). width sets minimum field width; flag = "-" left-aligns within the field. Unlike sprintf(), formatC() does not support embedding multiple values in a template string — it formats one value at a time.