formatC(x, digits = NULL, width = max(0, digits) + 1, format = NULL, flag = "", mode = NULL) format.char(x, width = NULL, flag = "-")
x
| an atomic numerical or character object, typically a vector of real numbers. |
digits
|
the desired number of digits after the decimal
point (format = "f" ) or significant digits
(format = "g" , = "e" or = "fg" ).
Default: 2 for integer, 4 for real numbers. If less than 0, the C default of 6 digits is used. |
width
|
the total field width; width < 0 means left
justify the number in this field (equivalent to flag ="-" ).
It is possible that the result will be longer than this, but that
should only happen in reasonable cases.
|
format
|
equal to "d" (for integers), "f" ,
"e" , "E" , "g" , "G" , "fg" (for
reals), or "s" (for strings).
|
flag
|
format modifier as in Kernighan and Ritchie, 2nd ed.,
page 243.
"0" pads leading zeros; "-" does left adjustment,
others are "+" , " " , and "#" .
|
mode
|
"double" (or "real" ), "integer" or
"character" .
Default: Automatic.
|
x
.
Unlike format
, each number is formatted individually. A for
loop over each element of x
, calling sprintf(...)
is
done in the C function str_signif
.
format.char(x)
and formatC
, for character x
, do
simple (left or right) padding with white space.
format
.xx <- pi * 10^(-5:4) options(digits = 4) # only for format cbind(format(xx), formatC(xx)) cbind(formatC(xx, wid = 9, flag = "-")) cbind(formatC(xx, dig = 5, wid = 8, format = "f", flag = "0")) format.char(c("a", "Abc", "no way"), wid = -7) # <=> flag = "-" formatC( c("a", "Abc", "no way"), wid = -7) # <=> flag = "-" formatC(c((-1:1)/0,c(1,100)*pi), wid=8, dig=1) xx <- c(1e-12,-3.98765e-10,1.45645e-69,1e-70,pi*1e37,3.44e4) ## 1 2 3 4 5 6 formatC(xx) formatC(xx, format="fg") # special "fixed" format. formatC(xx, format="f", dig=80)#>> also long strings