How to add trailing zeros to a DT table using `options` parameter?

Hello everyone,
I'm struggling with this problem, I want to add trailing zeros to a dt table but I need to do it with the options parameter, I'm aware of %>% format_() functions but I can't use this solution on my specific application, so I have the hope that this can be done with some JS() code inside columnDefs, I hope someone could help me.

This would be my desired output but keeping the class of the variables as numeric because I need to perform some calculations later.

library(DT)
library(dplyr)

iris %>% 
  mutate_if(is.numeric, ~sprintf(., fmt="%#.2f")) %>%
  datatable()

I found the solution

library(DT)
library(dplyr)

iris %>%
  datatable(options = list(columnDefs = list(list(targets = 1:2, 
                                                  render = JS(
                                                  "function(data, type, row, meta) {
                                                    if (type != 'display') {
                                                      return data;
                                                    }
                                                  return data.toFixed(2);
                                                  }")
                                                  )
                                             )
                           )
            )

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.