ro: Neat rounding

View source: R/ro.R

roR Documentation

Neat rounding

Description

Rounds a given number to given number of digits after the decimal point, returning it as string, with trailing zeros when applicable.

Usage

ro(num, round_to = 2, leading_zero = TRUE, signi = FALSE)

Arguments

num

Number to be rounded.

round_to

Number of fractional digits (i.e., digits after the decimal point), to round to.

leading_zero

Logical, TRUE by default. If FALSE, omits leading zero (e.g. returns ".17" instead of "0.17").

signi

Logical, FALSE by default. If TRUE, rounds to a fractional digit that allows at least the first N non-zero digits displayed in all numbers, where N is specified by the round_to parameter. (See base:formatC)

Value

Number as string: num rounded to round_to digits, with trailing zeros when applicable.

Examples

ro( 1.2345 ) # returns "1.23"

ro( 0.12345, 1 ) # returns "0.1"

ro( 12.3, 4 ) # returns "12.3000"

# examples with vectors
to_round = c(1000, 100, 0.1, 0.01, 0.001, 0.0001)
ro(to_round)
ro(to_round, 3)
ro(to_round, 3, leading_zero = FALSE)
ro(to_round, 3, signi = TRUE)
to_round2 = c(1230.000, 100, 0.012, 0.01, 0.123, 0.012340)
ro(to_round2, 3)
ro(to_round2, 3, signi = TRUE)
ro(to_round2, 2, signi = TRUE)
ro(to_round2, 1, signi = TRUE)
ro(to_round2, 9, signi = TRUE)



gasparl/neatstats documentation built on Jan. 10, 2023, 6:23 a.m.