Skip to contents

Trunc numbers with a specified number of decimals.

Usage

obs_trunc(n, dec)

Arguments

n

Numeric number

dec

Integer, number of decimals

Note

source https://stackoverflow.com/a/47015304/2418532

Examples

{
# in bash:
# printf "%07.4f" 72.05785
# results in 72.0578
# but:
formatC(72.05785, digits = 4, width = 8, format = "f", flag = "0")
# results in
"072.0579"
# the goal is to obtain the same trunc number as using bash, then:
formatC(obs_trunc(72.05785, 4),
        digits = 4,
        width = 8,
        format = "f",
        flag = "0")
}
#> [1] "072.0578"