cube icon indicating copy to clipboard operation
cube copied to clipboard

How to round off numbers to two decimal values

Open devopsprosiva opened this issue 6 years ago • 3 comments

Hi,

I'm using a postgresql database. I'm calculating a percentage measure. How can I round it to 2 decimals?

devopsprosiva avatar Jan 16 '20 08:01 devopsprosiva

@devopsprosiva You can use Postgres round function:

measures: {
  percentage: {
    sql: `round(${CUBE}.a / ${CUBE}.b, 2)`,
    type: `number`
  }
}

paveltiunov avatar Jan 16 '20 18:01 paveltiunov

It works. Thanks for the help. I'll close the ticket.

devopsprosiva avatar Jan 18 '20 20:01 devopsprosiva

In case of any similar issues with float variables, remember Postgres has some troubles with round. For me worked:

orderPrice: {
      sql: `ROUND(order_price::numeric,2)`,
      type: `sum`,
    }

Described here: https://stackoverflow.com/questions/13113096/how-to-round-an-average-to-2-decimal-places-in-postgresql

krystian50 avatar May 23 '20 11:05 krystian50