Learn R Programming

crosstable (version 0.3.2)

format_fixed: Format numbers with the exact same number of decimals, including trailing zeros

Description

Format numbers with the exact same number of decimals, including trailing zeros

Usage

format_fixed(
  x,
  digits = 1,
  zero_digits = 1,
  date_format = NULL,
  percent = FALSE,
  only_round = getOption("crosstable_only_round", FALSE),
  ...
)

Arguments

x

a numeric vector to format

digits

number of decimals

zero_digits

number of significant digits for values rounded to 0 (can be set to NULL to keep the original 0 value)

date_format

if x is a vector of Date or POSIXt, the format to apply (see strptime for formats)

percent

if TRUE, format the values as percentages

only_round

if TRUE, format_fixed simply returns the rounded value. Can be set globally with options("crosstable_only_round"=TRUE).

...

unused

Value

a character vector of formatted numbers

Examples

Run this code
# NOT RUN {
x = c(1, 1.2, 12.78749, pi, 0.00000012)
format_fixed(x, digits=3) #default zero_digits=1
format_fixed(x, digits=3, zero_digits=2)
format_fixed(x, digits=3, zero_digits=NULL)

x_sd = sd(iris$Sepal.Length/10000, na.rm=TRUE)
format_fixed(x_sd, dig=6)
format_fixed(x_sd, dig=3, zero_digits=2) #default only_round=FALSE
format_fixed(x_sd, dig=3, zero_digits=2, only_round=TRUE)
options("crosstable_only_round"=TRUE)
format_fixed(x_sd, dig=3, zero_digits=2) #override default
options("crosstable_only_round"=NULL)

x2 = mtcars$mpg/max(mtcars$mpg)
x2 = c(0.01, 0.1001, 0.500005, 0.00000012)
format_fixed(x2, percent=TRUE, dig=6)
# }

Run the code above in your browser using DataLab