Re: stored proc - how to format numbers?

From: Raymond O'Donnell <rod(at)iol(dot)ie>
To: "J(dot)V(dot)" <jvsrvcs(at)gmail(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: stored proc - how to format numbers?
Date: 2011-09-21 18:45:07
Message-ID: 4E7A30B3.1090501@iol.ie
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On 21/09/2011 19:36, J.V. wrote:
> In PostgreSQL (9.x), I have a variable that I want to format with commas.
>
> I do not know how many digits will be in the number, it could be an int
> (2Billion+) or a BigInt (18 digits or more).
>
> All I simply want to do is to use: to_number( and have the commas put
> in before display.

You're looking for to_char():

http://www.postgresql.org/docs/9.1/static/functions-formatting.html

Something like this ought to do what you want:

select to_char(12345678, 'FM999,999,990');

Each 9 in the formatting pattern will output a digit only if there's a
corresponding one in the number; and the FM trims off any padding
spaces. Just make sure you have enough 9s in the pattern for any
possible number you'll encounter.

Ray.

--
Raymond O'Donnell :: Galway :: Ireland
rod(at)iol(dot)ie

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Merlin Moncure 2011-09-21 18:58:34 Re: Random multiple times
Previous Message J.V. 2011-09-21 18:36:02 stored proc - how to format numbers?