In Postgres 9.5:
select to_char(111, 'FM9,999'); -- gives 111
select to_char(1111, 'FM9,999'); -- gives 1,111
select to_char(11111, 'FM9,999'); -- gives #,### !!!
How can I format my numbers with commas without advance knowledge/assumption of maximum number of possible digits preferably by using built-in stuff like above?
When working with Postgres, you can use the to_char() function to output numbers in a given format. The way it works is that you provide two arguments. The first argument is the number to be formatted. The second argument determines how it is formatted.
In English, we use commas to separate numbers greater than 999. We use a comma every third digit from the right.
A decimal separator is a symbol used to separate the integer part from the fractional part of a number written in decimal form (e.g., "." in 12.45).
How to Use ROUND() Function in PostgreSQL? To avail the functionalities of the ROUND() function, you have to follow the below syntax: ROUND(Number [ , n]); Here, in this syntax, the “Number” represents a numeric value to be rounded.
Add another digit or the maximum possible value:
select to_char(11111, 'FM9,999,999');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With