0

I would like to format numbers as percentages in cartodb. What are the best functions to display numbers with decimals, comma separators or percentages?

2

There is no 'percentage' type so you need to use a string You can do this by taking a numeric column and concatenating || with a %.

SELECT percent_column::text || '%' FROM table

You can also format the number first if you need, for example, multiply a decimal by 100 or round to sig digits,

SELECT round(percent_column * 100, 2)::text || '%' FROM table

For including commas, it is the same, you must turn numeric columns into strings. You could use this example,

SELECT TO_CHAR(76543210.98, '999G999G990D00')

Which was provided here, https://stackoverflow.com/questions/3360752/how-to-set-thousend-separator-for-postgresql

or more generically

SELECT TO_CHAR(number_column, '999G999G990D00') FROM table

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.