Cash Office in R

I would like to know how I can work with money with R. This means arithmetic, well-printed numbers, etc.

For example, I have several values

1.222.333,37 1.223.444,88 

I could translate it to numerical and round it by removing cents, but there is no better template for the job? I tried the format method, for example:

 format(141103177058,digits=3,small.interval=3,decimal.mark='.',small.mark=',') 

but without success. any advice or ideas?

+4
source share
3 answers

The weight package has a function for this: dollar_format ()

 install.packages("scales") library(scales) muchoBucks <- 15558.5985121 dollar_format()(muchoBucks) [1] "$15,558.60" 
+5
source

Suppose we have two special character values ​​(currency):

 s1 <- "1.222.333,37" s2 <- "1.223.444,88" 

First of all, we want R to display numerical values ​​with the correct number of digits:

 # controls representation of numeric values options(digits=10) 

Converting a currency to a numerical value can be implemented as follows:

 # where s is character moneyToDouble <- function(s){ as.double(gsub("[,]", ".", gsub("[.]", "", s))) } x <- moneyToDouble(s1) + moneyToDouble(s2) x 

Printing a numeric value in the form of a currency:

 # where x is numeric printMoney <- function(x){ format(x, digits=10, nsmall=2, decimal.mark=",", big.mark=".") } printMoney(x) 
+4
source

How about this:

 printCurrency <- function(value, currency.sym="$", digits=2, sep=",", decimal=".") { paste( currency.sym, formatC(value, format = "f", big.mark = sep, digits=digits, decimal.mark=decimal), sep="" ) } printCurrency(123123.334) 
+4
source

Source: https://habr.com/ru/post/1266177/


All Articles