1

I'm trying to format a table in a shiny app. I'm using the datatable function in the DT package. I have columns with values like xxx.yyy, x.y, x.yyyyy, x. I'd like to format these as xxx.yy, x.y0, x.yy, x.00. The formatRound('colName', 'digits') function takes care of xxx.yyy (formatRound(x = xxx.yyy, 2) but doesn't add the trailing zero(s).

The formatRound (and other format* functions in DT) are wrappers for some javascript code. Is there some addition to this code that would pad with zeros?

JerryN
  • 2,356
  • 1
  • 15
  • 49
  • I think I can help but can you please ask with a [reproducible](https://stackoverflow.com/q/5963269/1422451) code/data per the [MCVE](https://stackoverflow.com/help/mcve) and [`r`](https://stackoverflow.com/tags/r/info) tag description? – Hack-R Aug 26 '18 at 16:53

1 Answers1

2

My default function for this is sprintf, and it works like this:

sprintf(mtcars$mpg, fmt="%#.2f")

The 2 specifies digits after the decimal.

[1] "21.00" "21.00" "22.80" "21.40" ...
Mako212
  • 6,787
  • 1
  • 18
  • 37
  • And if you want to align on the decimal this code does the trick. It does right align, but since all numbers have the same number of digits after the decimal, that also means the decimal is aligned. `columnDefs = list(list(className = 'dt-right', targets = 3:5)) # Note target numbers start at column 0, so this does columns 4 to 6`. – JerryN Aug 26 '18 at 20:41