Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated default dp for pretty_num #93

Merged
merged 6 commits into from
Oct 17, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions R/pretty.R
cjrace marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ pretty_time_taken <- function(start_time, end_time) {
#' assign + or - based on the value
#' @param gbp whether to add the pound symbol or not, defaults to not
#' @param suffix suffix for the value, e.g. "%"
#' @param dp number of decimal places to round to, 2 by default
#' @param dp number of decimal places to round to, 0 by default
#' @param ignore_na whether to skip function for strings that can't be
#' converted and return original value
#' @param alt_na alternative value to return in place of NA, e.g. "x"
Expand Down Expand Up @@ -223,7 +223,7 @@ pretty_num <- function(
prefix = "",
gbp = FALSE,
suffix = "",
dp = 2,
dp = 0,
ignore_na = FALSE,
alt_na = FALSE,
nsmall = NULL) {
Expand Down
4 changes: 2 additions & 2 deletions man/pretty_num.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 17 additions & 10 deletions tests/testthat/test-pretty_num.R
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
test_that("prettifies", {
expect_equal(pretty_num(1, gbp = TRUE, suffix = " offer"), "£1.00 offer")
expect_equal(pretty_num(-1), "-1.00")
expect_equal(pretty_num(-1, prefix = "-"), "--1.00")
expect_equal(pretty_num(-1, prefix = "+/-"), "-1.00")
expect_equal(pretty_num(1, prefix = "+/-"), "+1.00")
expect_equal(pretty_num(12.289009, suffix = "%"), "12.29%")
expect_equal(pretty_num(1000), "1,000.00")
expect_equal(pretty_num(1, gbp = TRUE, suffix = " offer"), "£1 offer")
expect_equal(pretty_num(-1, dp = 2), "-1.00")
expect_equal(pretty_num(-1), "-1")
expect_equal(pretty_num(-1, prefix = "-"), "--1")
expect_equal(pretty_num(-1, prefix = "+/-"), "-1")
expect_equal(pretty_num(-1, dp = 2, prefix = "+/-"), "-1.00")
expect_equal(pretty_num(1, prefix = "+/-"), "+1")
expect_equal(pretty_num(12.289009, dp = 2, suffix = "%"), "12.29%")
expect_equal(pretty_num(1000), "1,000")
expect_equal(pretty_num(11^8, gbp = TRUE, dp = -1), "£210 million")
expect_equal(pretty_num(11^9, gbp = TRUE, dp = 3), "£2.358 billion")
expect_equal(pretty_num(-11^8, gbp = TRUE, dp = -1), "-£210 million")
expect_equal(pretty_num(-123421421), "-123.42 million")
expect_equal(pretty_num(-123421421, dp = 2), "-123.42 million")
expect_equal(pretty_num(63.71, dp = 1, nsmall = 2), "63.70")
expect_equal(pretty_num(894.1, dp = 2, nsmall = 3), "894.100")
expect_equal(
pretty_num(11^8, prefix = "+/-", gbp = TRUE, dp = -1.00), "+£210 million"
pretty_num(11^8, prefix = "+/-", gbp = TRUE, dp = -1), "+£210 million"
)
})

Expand All @@ -27,11 +29,16 @@ test_that("handles NAs", {
test_that("tests multiple values", {
expect_equal(
pretty_num(c(1:4)),
c("1.00", "2.00", "3.00", "4.00")
c("1", "2", "3", "4")
)

expect_equal(
pretty_num(c(1:4), nsmall = 1),
c("1.0", "2.0", "3.0", "4.0")
)

expect_equal(
pretty_num(c(1.478, 7.38897, 8.37892), dp = 1, nsmall = 2),
c("1.50", "7.40", "8.40")
)
})
19 changes: 17 additions & 2 deletions tests/testthat/test-pretty_num_table.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,36 @@ df <- data.frame(


test_that("prettifies tables", {
expect_equal(pretty_num_table(df), data.frame(
expect_equal(pretty_num_table(df, dp = 2), data.frame(
a = c("2.59", "-5.89", as.double(NA)),
b = c("11.20", "45.69", "-78.50"),
c = c(as.double(NA), as.double(NA), as.double(NA))
))

expect_equal(pretty_num_table(df, dp = 3), data.frame(
a = c("2.589", "-5.894", as.double(NA)),
b = c("11.199", "45.689", "-78.499"),
c = c(as.double(NA), as.double(NA), as.double(NA))
))

expect_equal(
pretty_num_table(df, gbp = TRUE, exclude_columns = "c"),
pretty_num_table(df, dp = 2, gbp = TRUE, exclude_columns = "c"),
data.frame(
a = c("£2.59", "-£5.89", as.double(NA)),
b = c("£11.20", "£45.69", "-£78.50"),
c = c("X", "Y", "Z")
)
)

expect_equal(
pretty_num_table(df, gbp = TRUE, exclude_columns = "c"),
data.frame(
a = c("£3", "-£6", as.double(NA)),
b = c("£11", "£46", "-£78"),
c = c("X", "Y", "Z")
)
)


expect_equal(
pretty_num_table(df,
Expand Down
Loading