R/round2.R

Defines functions round2

Documented in round2

#' Standard rounding rather than R's round function which uses bankers rounding
#'
#' @param x Number to be rounded
#' @param n Decimal places to be rounded to
#' 
#' @return The number rounded to the nearest integer where .5 is always rounded up
#'
#' @export
#'
#' @examples
#' x <- 2.3
#' n <- 0
#' round2(x, n)
#' 
#' x <- 1.85
#' n <- 1
#' round2(x, n)
#' 
#' x <- 10.988
#' n <- 2
#' round2(x, n) 
round2 <- function(x, n) {
  posneg <- sign(x)
  z <- abs(x) * 10 ^ n + 0.5
  z <- trunc(z)
  z <- z / 10 ^ n
  z * posneg
}
Tychobra/tychobratools documentation built on April 12, 2022, 12:29 a.m.