diff --git a/R/round_five_up.R b/R/round_five_up.R index 0e02073..56e795e 100644 --- a/R/round_five_up.R +++ b/R/round_five_up.R @@ -1,33 +1,51 @@ #' Round five up #' +#' @description #' Round any number to a specified number of places, with 5's being rounded up. -#' This is as an alternative to round in base R, which sometimes rounds 5's -#' down due to an international standard. For more information see the -#' round() documentation: -#' https://stat.ethz.ch/R-manual/R-devel/library/base/html/Round.html +#' +#' @details +#' Rounds to 0 decimal places by default. #' #' You can use a negative value for the decimal places. For example: #' -1 would round to the nearest 10 #' -2 would round to the nearest 100 #' and so on. #' -#' @param value number to be rounded -#' @param dp number of decimal places to round to +#' This is as an alternative to round in base R, which uses a bankers round. +#' For more information see the +#' [round() documentation](https://rdrr.io/r/base/Round.html). +#' +#' +#' @param number number to be rounded +#' @param dp number of decimal places to round to, default is 0 #' #' @return Rounded number #' @export #' #' @examples -#' round_five_up(2495, -1) -#' round_five_up(2495.85, 1) -round_five_up <- function(value, dp) { - if (!is.numeric(value) && !is.numeric(dp)) stop("both inputs must be numeric") - if (!is.numeric(value)) stop("the value to be rounded must be numeric") - if (!is.numeric(dp)) stop("the decimal places value must be numeric") +#' # No dp set +#' round_five_up(2485.85) +#' +#' # With dp set +#' round_five_up(2485.85, 2) +#' round_five_up(2485.85, 1) +#' round_five_up(2485.85, 0) +#' round_five_up(2485.85, -1) +#' round_five_up(2485.85, -2) +round_five_up <- function(number, dp = 0) { + if (!is.numeric(number) && !is.numeric(dp)) { + stop("both input arguments must be numeric") + } + if (!is.numeric(number)) { + stop("the input number to be rounded must be numeric") + } + if (!is.numeric(dp)) { + stop("the decimal places input must be numeric") + } - z <- abs(value) * 10^dp + z <- abs(number) * 10^dp z <- z + 0.5 + sqrt(.Machine$double.eps) z <- trunc(z) z <- z / 10^dp - return(z * sign(value)) + return(z * sign(number)) } diff --git a/man/round_five_up.Rd b/man/round_five_up.Rd index e32006d..2f3dc14 100644 --- a/man/round_five_up.Rd +++ b/man/round_five_up.Rd @@ -4,30 +4,39 @@ \alias{round_five_up} \title{Round five up} \usage{ -round_five_up(value, dp) +round_five_up(number, dp = 0) } \arguments{ -\item{value}{number to be rounded} +\item{number}{number to be rounded} -\item{dp}{number of decimal places to round to} +\item{dp}{number of decimal places to round to, default is 0} } \value{ Rounded number } \description{ Round any number to a specified number of places, with 5's being rounded up. -This is as an alternative to round in base R, which sometimes rounds 5's -down due to an international standard. For more information see the -round() documentation: -https://stat.ethz.ch/R-manual/R-devel/library/base/html/Round.html } \details{ +Rounds to 0 decimal places by default. + You can use a negative value for the decimal places. For example: -1 would round to the nearest 10 -2 would round to the nearest 100 and so on. + +This is as an alternative to round in base R, which uses a bankers round. +For more information see the +\href{https://rdrr.io/r/base/Round.html}{round() documentation}. } \examples{ -round_five_up(2495, -1) -round_five_up(2495.85, 1) +# No dp set +round_five_up(2485.85) + +# With dp set +round_five_up(2485.85, 2) +round_five_up(2485.85, 1) +round_five_up(2485.85, 0) +round_five_up(2485.85, -1) +round_five_up(2485.85, -2) } diff --git a/tests/testthat/test-round_five_up.R b/tests/testthat/test-round_five_up.R index 01f8389..a8d4a13 100644 --- a/tests/testthat/test-round_five_up.R +++ b/tests/testthat/test-round_five_up.R @@ -1,32 +1,38 @@ test_that("Rounds fives up", { expect_equal(round_five_up(285, -1), 290) + expect_equal(round_five_up(2.85), 3) + expect_equal(round_five_up(2.5), 3) expect_equal(round_five_up(2.85, 1), 2.9) }) test_that("Rounds other numbers", { - expect_equal(round_five_up(283, -1), 280) - expect_equal(round_five_up(2.87, 1), 2.9) + expect_equal(round_five_up(283.54, -2), 300) + expect_equal(round_five_up(283.54, -1), 280) + expect_equal(round_five_up(283.234, 0), 283) + expect_equal(round_five_up(283.234), 283) + expect_equal(round_five_up(2.86, 1), 2.9) + expect_equal(round_five_up(2.86, 2), 2.86) }) test_that("Input validation", { expect_error( round_five_up("ten", "10"), - "both inputs must be numeric" + "both input arguments must be numeric" ) expect_error( round_five_up(12, "ten"), - "the decimal places value must be numeric" + "the decimal places input must be numeric" ) expect_error( round_five_up(12, "10"), - "the decimal places value must be numeric" + "the decimal places input must be numeric" ) expect_error( round_five_up("twelve", 10), - "the value to be rounded must be numeric" + "the input number to be rounded must be numeric" ) expect_error( round_five_up("12", 10), - "the value to be rounded must be numeric" + "the input number to be rounded must be numeric" ) })