Numeric precision / formatting for values in a custom viewer?

I’m working on a custom viewer – and, when displaying numeric data, I use the Number.toPrecision(k) method from Javascript to display the values with a certain number k of significant digits.

However, I would like to be able to match this value, k, to whatever is being used in the main grid viewer. Is there a way to read any parameters, that are set globally in Datagrok or specifically for the grid viewer, for displaying numeric values? Or is there a way to retrieve a javascript function that performs the same formatting as occurs in the grid?

Hi Timothy, welcome, I’m glad to see you here!

Here’s an example that lets you get or set column format for the grid:

Unfortunately, the format returned is not generally compatible with the JS formatting routines, although many options work out of the box (for example, “currency” works but “compact simple currency” doesn’t). We will expose the necessary formatting-related functions in the JS API. The column method getString might be what you were looking for: it returns the i-th value of a column as a string, taking into account the value format defined for that column. I’ve played around with it both on dates and numbers, it works fine, although there might be some edge cases I haven’t considered. Assuming you are running this from the browser console to quickly see the outcome when changing the column formatting from the UI:

let col ='columnName');
let formattedValue = col.getString(0); // the first column value with the right formatting
1 Like