padZeros: Pad Numbers with Leading Zeros

View source: R/padZeros.R

padZerosR Documentation

Pad Numbers with Leading Zeros

Description

Converts numeric values to character strings with leading zeros to achieve a specified total width.

Usage

padZeros(x, nchars)

Arguments

x

A numeric vector of integers to be padded with leading zeros.

nchars

An integer specifying the total number of characters in the output string (including leading zeros).

Value

A character vector of the same length as x, with each element padded with leading zeros to reach nchars characters.

Examples

# Pad single digit numbers to 3 characters
padZeros(5, 3)
# Returns: "005"

# Pad a vector of numbers
padZeros(c(1, 10, 100), 4)
# Returns: c("0001", "0010", "0100")

# Useful for creating sequential file names
paste0("file_", padZeros(1:5, 3), ".txt")
# Returns: c("file_001.txt", "file_002.txt", ..., "file_005.txt")

# Or append to an existing file name
NMdata::fnAppend("file.txt", padZeros(1:5, 3), collapse=NULL)


tracee documentation built on Nov. 24, 2026, 5:07 p.m.