Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Comma separating numbers without advance knowledge of number of digits in Postgres

Tags:

postgresql

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?

like image 279
mehmet Avatar asked Mar 19 '18 19:03

mehmet


People also ask

How to format numbers in PostgreSQL?

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.

How do you split numbers with commas?

In English, we use commas to separate numbers greater than 999. We use a comma every third digit from the right.

What are the commas between numbers called?

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 do I round in PostgreSQL?

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.


1 Answers

Add another digit or the maximum possible value:

select to_char(11111, 'FM9,999,999');
like image 102
McNets Avatar answered Sep 29 '22 01:09

McNets