1

I am trying to build a dashboard in R using Shiny , in one of the value box outputs i have a number displayed. I want to change the formatting as per indian number system. The value box output currently shows the number as 123,456,789 and I want to represent it like this 12,34,56,789.

Please let me know if any more information be required , any help would be much appreciated.

ABHISHEKK
  • 11
  • 1
  • @r2evans can you guide me please on how to go about it. If you can list out the steps please. – ABHISHEKK May 27 '20 at 06:05
  • I'm not familiar with the Indian formatting. How are decimals printed? (I can't find a ready reference for it, just one for integers.) – r2evans May 27 '20 at 06:17
  • The decimals are printed normally at least according to [wikipedia](https://en.wikipedia.org/wiki/Indian_numbering_system) the problem is those last three digits I took the liberty to raise a feature request, this is an accessibility option in my opnion here is the link https://github.com/r-lib/scales/issues/274 – Bruno May 27 '20 at 06:21
  • @r2evans For my current requirement i am limiting the number of significant digits to the right of the decimal to one hence what we have on the right of the decimal is not an issue as of now. On the left of the decimal i want the formatting to be of the sorts 12,34,56,789. Does this help explain the problem ? – ABHISHEKK May 27 '20 at 06:40

1 Answers1

0

Here's a quick hack that allows further use of format for most other functionality. The only intervention it does is check if there are significant digits to warrant

format2 <- function(x, ..., big.mark = "", big.interval = c(3L, 2L), decimal.mark = ".") {
  intervene <- (log(abs(x), 10) >= sum(big.interval)) && nzchar(big.mark)
  cl <- match.call()
  cl[[1]] <- substitute(format)
  if (intervene) {
    cl$x <- x %/% 10^big.interval[1]
    cl$big.interval <- big.interval[2]
    bigx <- eval.parent(cl)
    cl$x <- x 
    cl$big.interval <- big.interval[1]
    mostx <- eval.parent(cl)
    mostx <- 
      substr(mostx,
             1L + nchar(x %/% 10^big.interval[1]) +
               trunc(trunc(log(abs(x %/% 10^big.interval[1]), 10L)) / big.interval[1]),
             nchar(mostx))
    return( paste0(bigx, mostx) )
  } else eval.parent(cl)
}

format2(123456789)
# [1] "123456789"
format2(12345, big.mark = ",") # no intervene necessary
# [1] "12,345"
format2(123456789, big.mark = ",")
# [1] "12,34,56,789"
format2(1e6 + pi, big.mark = ",")
# [1] "10,00,003"
format2(1e6 + pi, big.mark = ",", digits = 22)
# [1] "10,00,003.1415926536"
format2(1e6 + pi, big.mark = ",", digits = 22, small.mark = ",")
# [1] "10,00,003.14159,26536"

Disclaimer: I certainly haven't tested this rigorously, so it's feasible that it might have corner-cases not covered. Caveat emptor.

r2evans
  • 141,215
  • 6
  • 77
  • 149