sprintf
Generate C-Style Formatted Output

Description

Combines numbers and strings into a formatted string.

Usage

sprintf(fmt, ...)
gettextf(fmt, ..., domain = NULL)

Arguments

fmt a format string as used by the standard C sprintf function. (Note that not all features are supported.) Each % (except %%) indicates that the start of a conversion specification for one of the ... arguments. See the Details section below for more information. If translation is enabled, the fmt string is translated by the function gettext when used in a call to gettextf.
... as in the C sprintf function, the arguments after the format string correspond to the % conversion specifications in the format string. Usually each conversion specification refers to one ... argument, but if the conversion specification contains asterisks (*), then each * refers to one ... argument.
domain the domain to use for language translation. This argument is ignored in this engine version.

Details

The conversion specifiers start with a % and end with one of the following conversion characters:
"s" string "d" signed decimal integer "i" signed decimal integer "o" octal integer "x" hexadecimal integer "X" hexadecimal integer, with capital A-F "f" floating point number; no scientific notation "g" floating point number; use e+exponent scientific notation if it would be shorter "G" floating point number; use E+exponent scientific notation if it would be shorter "e" floating point number; e+exponent scientific notation "E" floating point number; E+exponent scientific notation
The conversion character can be preceded by minwidth.precision where minwidth and precision are both integers and neither must be present. Their precise meaning depends on the conversion character, but usually minwidth gives the minimum number of characters to use and precision gives details like the number of significant digits, the number of digits after the decimal point, or the maximum number of characters to use. See a C language manual for details.
Either minwidth or precision (but not both) can be given as "*", indicating to use the next argument in the ... list as the value. Thus, one conversion specification can use one or two arguments from the ... list, depending the presence of the asterisk.
The conversion character (along with possible minwidth.precision) can be preceded by single character flags, whose meaning depends on the conversion character. For example, for "d", a "0" means to fill the space before the most significant digit with 0s and a "+" means to precede positive numbers with a plus sign. See a C language manual for details.
The C standard allows the % specifier to start with "%n\$" for a number "n", meaning that the specifier refers to the nth ... argument.
Because Native Language Support and support for domains are not included in this version, calling gettextf gives the same result as calling sprintf.
Value
returns a vector of formatted strings. The length of the output vector is the length of the longest argument; shorter arguments are recycled to the length of the longest. However, if any argument is 0 long, the output is also 0 long.
See Also
format, cat, gettext, paste.
Examples
sprintf("pi is about %4.2f", asin(1)*2)
# [1] "pi is about 3.14"

sprintf("Hex for decimal %2d is 0x%02x", c(3,19,40), c(3,19,40)) # [1] "Hex for decimal 3 is 0x03" "Hex for decimal 19 is 0x13" # [3] "Hex for decimal 40 is 0x28"

# vector for formats, one number to format sprintf(c("%d","%x","%o"), 46) # [1] "46" "2e" "56"

# vary number of significant digits sprintf("%12.*g", 2:10, exp(10)) # [1] " 2.2e+04" " 2.2e+04" # [3] " 2.203e+04" " 22026" # [5] " 22026.5" " 22026.47" # [7] " 22026.466" " 22026.4658" # [9] " 22026.46579"

cat(sep="\n", sprintf("\"%4.4s\" is 4 letter abbreviation for %1$s.", c("A","Alabama"))) # " A" is 4 letter abbreviation for A. # "Alab" is 4 letter abbreviation for Alabama.

Package base version 6.0.0-69
Package Index