diff --git a/NEWS.md b/NEWS.md index d3dc32ed..2ba530cf 100644 --- a/NEWS.md +++ b/NEWS.md @@ -2,6 +2,8 @@ ## Bug fixes +* Fixed rounding issue in round_half_up() function (#396, thanks to @JJSteph) + * Warnings for incomplete argument names are fixed (fix #367, thanks to @pabecerra for reporting and @billdenney for fixing) * 3-way tabyls with factors have columns and rows sorted in the correct order, by factor level (#379). diff --git a/R/round_half_up.R b/R/round_half_up.R index 03e169e9..7a4e69c6 100644 --- a/R/round_half_up.R +++ b/R/round_half_up.R @@ -17,7 +17,7 @@ round_half_up <- function(x, digits = 0) { posneg <- sign(x) z <- abs(x) * 10 ^ digits - z <- z + 0.5 + z <- z + 0.5 + sqrt(.Machine$double.eps) z <- trunc(z) z <- z / 10 ^ digits z * posneg diff --git a/tests/testthat/test-utilities.R b/tests/testthat/test-utilities.R index 7370dfd5..bd7ae77e 100644 --- a/tests/testthat/test-utilities.R +++ b/tests/testthat/test-utilities.R @@ -10,4 +10,5 @@ test_that("round_half_up works", { expect_equal(round_half_up(0.5, 0), 1) expect_equal(round_half_up(1.125, 2), 1.13) expect_equal(round_half_up(1.135, 2), 1.14) -}) \ No newline at end of file + expect_equal(round_half_up(2436.845, 2), 2436.85) +})