Rounding numbers in fields/lists

  • 1
  • Question
  • Updated 6 years ago
  • Answered
Many numeric and currency fields are by default using 2 decimals. However in many instances a truncated display showing only the integer part would look better in tables or field editor. How can this be done with SKUID?
Photo of Peter Baeza

Peter Baeza

  • 2,868 Points 2k badge 2x thumb

Posted 6 years ago

  • 1
Photo of Zach McElrath

Zach McElrath, Employee

  • 53,702 Points 50k badge 2x thumb
Official Response
The best way to do it would be to use a custom field renderer on those fields that you want to round to just the integer part / truncate. This field renderer will run all of the standard rendering logic, but will first override the metadata of the given field so that it will render as an integer.

Here's a Gist containing the code for this snippet... or see below.

Here's the body of the inline snippet:

var field = arguments[0],
value = arguments[1],
metadata = field.metadata,
dt = metadata.displaytype;

// For currency, double, and percent fields,
// manually override the field's metadata
// related to how many decimal points to display

if (dt == 'CURRENCY' || dt == 'DOUBLE' || dt == 'PERCENT') {
metadata.scale = 0;

// Run the field's default renderer