Rounding Using Round Away From 0 Method
round_away_0.Rdround_away_0 takes a numeric vector, rounds them to a specified digit amount using the round away from 0 method for ties (i.e. 1.5). This is the SAS method for rounding.
Value
if trailing_zeros = TRUE returns a character vector of rounded values with trailing zeros, otherwise returns a numeric vector of rounded values.
Examples
vals_to_round = c(NA,-3.5:3.5,NA)
# [1] NA -3.5 -2.5 -1.5 -0.5 0.5 1.5 2.5 3.5 NA
# round() will round to even numbers when tied at X.5
round(vals_to_round)
#> [1] NA -4 -2 -2 0 0 2 2 4 NA
# [1] NA -4 -2 -2 0 0 2 2 4 NA
# round_away_0() will round away from 0 when tied at X.5
round_away_0(vals_to_round)
#> [1] NA -4 -3 -2 -1 1 2 3 4 NA
# [1] NA -4 -3 -2 -1 1 2 3 4 NA
# Can force trailing zeros (will output character vector)
round_away_0(vals_to_round, digits = 2, trailing_zeros = TRUE)
#> [1] NA "-3.50" "-2.50" "-1.50" "-0.50" "0.50" "1.50" "2.50" "3.50"
#> [10] NA