Processing Negative Number in "Accounting" Format

processing negative number in accounting format

If you create an "as.acntngFmt" method for the accounting format, you can read (or perhaps re-read with a text connection using colClasses("acnt").

 setClass("acntngFmt")
# [1] "acntngFmt"
setAs("character", "acntngFmt",
function(from) as.numeric( gsub("\\)", "", gsub("\\(", "-", from))))

Input <- "A, B, C
(1.76), 1%, 3.50€
2.00, 2%, 4.77€
3.000, 3% , €5.68"

DF <- read.csv(textConnection(Input), header = TRUE,
colClasses = c("acntngFmt", "character", "character"))
str(DF)
'data.frame': 3 obs. of 3 variables:
$ A: num -1.76 2 3
$ B: chr "1%" "2%" "3%"
$ C: chr "3.50€" "4.77€" "€5.68"

Accounting Style string format in ASP.NET

Ignoring your alignment requirements, you could use

number.ToString("€#,##0.00;(€#,##0.00);Zero")

to bracket negative numbers.

To align your numbers, you'd have to format without the currency symbol, and pad the formatted numbers yourself with spaces, using a fixed width font would make this job easier for you.

EDIT:

It seems String.Format is your friend:

String.Format("{0,15:#,##0.00 ;(#,##0.00);-   }", number)

where 15 is the total width of the output, and you need to append this text to your currency symbol. (Again, this aligns in fixed width only)

How to format a data table or matrix in R for required currency symbol and negative numbers in parentheses ()

It's pretty easy using sprintf and ifelse. You can use two matrices of format strings to give each row a different format for positive vs negative values.

Edit: If you want comma separators in large numbers you can use format, but then you are working with strings, so I think it's easier to build the output matrices by line for positive and negative numbers separately.

# data
results.table <- matrix(2000*(runif(20)-runif(20)), nrow=5, ncol=4)
results.table[1,] <- results.table[1,] - round(results.table[1,], 0)
row.names(results.table) <- c("Combined PP Difference", "Medical Cost Difference (in $)", "Total PP Difference (in $)",
"YYL based on Med Cost ($/YYL)", "YYL based on Total Cost ($/YYL)")
colnames(results.table) <- c(" Drug A vs P", " Drug A vs Q", " Drug A vs R", " Drug A vs S")

# formatted for positive and negative
fp <- matrix(nrow=5, ncol=4)
fp[1,] <- sprintf("%s", format(round(results.table[1,], 4), big.mark=",", scientific=FALSE, trim=TRUE))
fp[2:3,] <- sprintf("$%s", format(round(results.table[2:3,], 0), big.mark=",", scientific=FALSE, trim=TRUE))
fp[4:5,] <- sprintf("$%s/YYL", format(round(results.table[4:5,], 0), big.mark=",", scientific=FALSE, trim=TRUE))
fn <- matrix(nrow=5, ncol=4)
fn[1,] <- sprintf("(%s)", format(abs(round(results.table[1,], 4)), big.mark=",", scientific=FALSE, trim=TRUE))
fn[2:3,] <- sprintf("$(%s)", format(abs(round(results.table[2:3,], 0)), big.mark=",", scientific=FALSE, trim=TRUE))
fn[4:5,] <- sprintf("$(%s)/YYL", format(abs(round(results.table[4:5,], 0)), big.mark=",", scientific=FALSE, trim=TRUE))

# formatted table
noquote(ifelse(results.table<0, fn, fp))

Drug A vs P Drug A vs Q Drug A vs R Drug A vs S
Combined PP Difference 0.1275 (0.4701) 0.2297 0.2005
Medical Cost Difference (in $) $472 $(141) $(927) $271
Total PP Difference (in $) $233 $(141) $891 $288
YYL based on Med Cost ($/YYL) $(1,425)/YYL $(160)/YYL $(759)/YYL $1,307/YYL
YYL based on Total Cost ($/YYL) $(377)/YYL $(1,222)/YYL $545/YYL $27/YYL

Created on 2019-04-12 by the reprex package (v0.2.1)

Negative currency values using parse_number in readr R

If you want to stay with tidyverse functions as per comment here you can just use stringr functions instead of gsub. Options like this:

library(tidyverse)
x <- c("$1,000.00", "$500.00", "-$200.00")
x %>%
str_replace("^-\\$(.*)$", "$-\\1") %>%
parse_number()
#> [1] 1000 500 -200

x %>%
str_remove("\\$") %>%
parse_number()
#> [1] 1000 500 -200

Created on 2018-05-03 by the reprex package (v0.2.0).

%( convert negative number to positive in System.out.printf


can somebody please explain why the -3 is printed as 3 in this statement?

Its isn't, it is being printed as (3)

From the Javadoc for Formatter say flag (

The result will enclose negative numbers in parentheses

Manipulating columns of numbers in elisp

I'd check out org-mode's table features; they probably do a lot of what you're trying to do.



Related Topics



Leave a reply



Submit