You can get pretty close with label_number_si():

pounds <- scales::label_number_si(prefix = "£")
pounds(10 ^ (0:7))
#> [1] "£1"    "£10"   "£100"  "£1K"   "£10K"  "£100K" "£1M"   "£10M"

<sup>Created on 2019-12-09 by the [reprex
package](https://reprex.tidyverse.org) (v0.3.0.9001)</sup>

Hadley

On Fri, Dec 6, 2019 at 5:51 AM POLWART, Calum (COUNTY DURHAM AND
DARLINGTON NHS FOUNDATION TRUST) via R-help <r-help@r-project.org>
wrote:
>
> I'm writing a quite large document in Rmarkdown which has financial data in 
> it.  I format that data using scales::dollar() currently something like this:
>
> >
> > require (scales)
> > x = 100000
> > cat (dollar (x, prefix ="£", big.mark=","))
>
> £100,000
>
> But actually, I'd quite like to get £100k out in that instance so I'd do:
>
> > cat (dollar (x/10^3, prefix ="£", suffix="k" ))
>
> £100k
>
> But x could be 100 or 10,000,000.  I want some form of 'automatic' version 
> that might give me something like:
>
> >
> > require (scales)
> > y = 0:7
> > x = 1^y
> > dollar(x, prefix="£")
> [1] "£1"          "£10"         "£100"        "£1,000"      "£10,000"     
> "£100,000"    "£1,000,000"  "£10,000,000"
>
> But what I want is more like:
>
> £1.00  £10.00  £100  £1k  £10k  £100k  £1m  £10m
>
> I'm sure I can write something as a little function, but before I do - is 
> there something already out there?
>
> I have a similar need to format milligrams through to kilograms.
>
>
> ********************************************************************************************************************
>
> This message may contain confidential information. If ...{{dropped:27}}

______________________________________________
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
  • ... POLWART, Calum (COUNTY DURHAM AND DARLINGTON NHS FOUNDATION TRUST) via R-help
    • ... Hadley Wickham

Reply via email to