format.float: Format numbers for printing

View source: R/format.float.R

format.floatR Documentation

Format numbers for printing

Description

format.float prepares numbers for results printing.

Usage

## S3 method for class 'float'
format(x, digits = 2)

Arguments

x

Value to be formatted. Input can be a numeric or character value.

digits

Number of fixed digits, after decimal point

Details

This function formats numbers with the given number of decimal digits. Numbers are always assumed to be double. If there is a percent sign in x, it is removed prior to formatting.

Value

Returns a character value.

Author(s)

Felipe Figueiredo

Examples

format.float(0.5)
format.float(0.672, 4)
format.float(1.4927, 1)
format.float(c(1.756, 1.823))
# Input is character
format.float("1.12543")
format.float(c("1.756", "1.823"))
# Input is percentage
format.float("10%", 1)
format.float("15 %", 1)

# Useful for data frames
DF <- data.frame(num = c(1.756, 1.823), char = c("1.756", "1.823"), stringsAsFactors = FALSE)
transform(DF, num = format.float(num), char = format.float(char))


philsf/philsfmisc documentation built on April 11, 2022, 9:04 p.m.