-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add default value to round five up (#62)
* add extra examples and default dp value * update error messages to be more specific * fix typo in round_five_up() documentation * style round_five_up() code * change argument from value to number * Tweak r5u wording based on PR comments to be easier to understand
- Loading branch information
Showing
3 changed files
with
63 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)) | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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" | ||
) | ||
}) |