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

Change dollar to print negative sign on the outside; allow percent to work with negative number #40

Merged
merged 8 commits into from Jun 12, 2015

Conversation

dougmitarotonda
Copy link
Contributor

I am not sure where the authoritative source on this would be, but how about:
http://english.stackexchange.com/questions/124797/how-to-write-negative-currency-in-text

This commit is to fix #38

@dougmitarotonda
Copy link
Contributor Author

Hmm, sorry for the newbie mistake. Apparently my commits for the changes for percent got bundled into the same pull request. Sorry about that.

Anyway, a505095 and 3120cc6 are to fix #39

This change was not obvious by just editing the `scales` package. Rather, when I tried using my edits with `ggplot2`, it turns out that `NA`s are created on the lower and upper bounds so this function needs to gracefully handle `NA`s.
Do this so `nsmall` works the same way for large negative numbers as well as positive.
@trevorld
Copy link

I personally prefer -$100.00 style negative numbers (slightly more common in an academic Economics article) but if you tweaked your dollar_format function you could also support the other commonly negative number style ($100.00) ( more common in accounting and Excel's currency default formatting style: http://www.quepublishing.com/articles/article.aspx?p=2024309&seqNum=9 ).

my_dollar_format <- function (largest_with_cents = 1e+05, negative_parentheses = FALSE) {
    old_dollar <- scales::dollar_format(largest_with_cents = largest_with_cents)
    new_dollar <- function(x) {
        x[!is.na(x)] <- old_dollar(x[!is.na(x)])
        if (negative_parentheses) {
            gsub("\\$-(.*)", "($\\1)", x)
        } else {
            gsub("\\$-", "-$", x)
        }
    }
}
> d <- c(100.02, 25.32, -24.42, NA)
> my_dollar_format()(d)
> [1] "$100.02" "$25.32"  "-$24.42" NA   
> my_dollar_format(negative_parentheses=TRUE)(d)
> [1] "$100.02"  "$25.32"   "($24.42)" NA

@dougmitarotonda
Copy link
Contributor Author

I like your suggestion to allow for the parentheses. I defer to the package authors on what to do here.

@@ -53,6 +53,50 @@ test_that("dollar format", {
"$600,000", "$700,000", "$800,000", "$900,000", "$1,000,000"))
expect_equal(dollar(c(9.999)), c("$10"))
expect_equal(dollar(c(99.999)), c("$100"))
expect_equal(dollar(-c(100, 0.23, 1.456565, 2e3)),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These tests seem a bit repetitive. Could you please reduce and break up into separately named tests?

@hadley
Copy link
Member

hadley commented Jun 10, 2015

I'd be happy to review a changes to add a use_parens argument

@hadley
Copy link
Member

hadley commented Jun 12, 2015

Hmmmm, this got a bit more complicated now that dollar_format() is more general - you can supply arbitrary prefixes and suffixes now, and while -$100 looks better, -USD 100 certainly doesn't. Maybe it's just better to implement that parens argument and leave $-100 as is?

@hadley hadley merged commit 578259c into r-lib:master Jun 12, 2015
@trevorld
Copy link

Well character currency descriptors like USD are usually supposed to come after the number, i.e. http://www.proz.com/forum/linguistics/283051-currency_usd_does_it_come_before_or_after_the_number.html so you would expect people to mainly use characters like USD as a suffix which gives you -500 USD in either case (assuming prefix is then "") instead of the more ugly -USD 500...

I think most people (including me and the original patch contributor doug) want negative currency values where the negative sign goes in front of the prefix like -$100 or - £100.45. Both of us have actually written custom formatters to let us do so since that is the format necessary to have good looking charts if you are an economist or business person. If for some reason they don't want it reversed then it should be straightforward to write a custom formatter like

my_formatter <- function(prefix, ...) {
   function(x) {
     my_dollar <- dollar_format(prefix=prefix, ...)
     values <- my_dollar(x)
     values <- gsub(paste0("-", prefix), paste0(prefix, "-"), values)
     values
   }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Using dollar with a negative number
3 participants