piersharding / RSAP

SAP NW RFC connector for R
21 stars 16 forks source link

Handling of negative currency values. #14

Closed t-perry closed 5 years ago

t-perry commented 5 years ago

I came across a bit of a weird one, and not sure if it's organisation specific, but in GLPCA the negative numbers come through as 15 character strings, and any negative values are displayed with the sign trailing the value. e.g. " 100.00-" What I was seeing happen is that the RSAPReadTable function is coercing these values and they come out as NAs, and therefore display as 0.

I did a quick but messy change (as I needed to get something functional quickly) and swapped the following chunk:

data[[f]] <- as.numeric(unlist(lapply(data[[f]], 
                FUN = function(x) {
                  sub("[^\\d\\.\\-\\,]", "", x, perl = TRUE)
                })))

With this: (had to load the DescTools package)

data[[f]] <- as.numeric(
        StrLeft(as.character(data[[f]]),n=-1)
      ) * as.numeric(
        paste0(
          StrRight(as.character(data[[f]]),n=1)
          , 1
        )
      )

Effectively splitting the two strings and pasting the sign (either "-" or " " to a 1 to multiply the value by) There may be a more elegant way to handle this, but keen to hear your thoughts?

t-perry commented 5 years ago

Wow, my bad, this is already fixed so just disregard me!