Skip site navigation (1) Skip section navigation (2)

Re: Function to spell out a number

From: Jasen Betts <jasen(at)xnet(dot)co(dot)nz>
To: pgsql-novice(at)postgresql(dot)org
Subject: Re: Function to spell out a number
Date: 2010-11-07 12:10:39
Message-ID: ib64vv$rc0$ (view raw, whole thread or download thread mbox)
Lists: pgsql-novice
On 2010-11-04, lists(at)kambanaria(dot)org <lists(at)kambanaria(dot)org> wrote:
> Hi everyone,
> I've checked the manual and a bit online but could not find this:
> Is there a provided function to spell out numbers in Postgres?

yes, cash_words(), it's deprecated.

> (in different languages if possible.)

not sure about that.

> For example:
> to_words(1234) => 'two hundred and thirty four"
> Kind regards:
> al_shopov

CREATE FUNCTION to_words(num integer) RETURNS text
AS $$
       str TEXT;
       str=cash_words(num * ('1.0'::money));
       str=REPLACE(str,' dollars and zero cents','');
       str=REPLACE(str,' dollar and zero cents','');
    RETURN lower(str); 
LANGUAGE plpgsql;
the type 'money' is deprecated, and the string 'dollar'
may be locale dependant so it's probably a bad idea to 
do this.  This is probably why cash_words is not 
mentioned in the 8.4 manual.

May be a better approach, but you'll still need find/write 
rulesets, and figure out how to call it from postgres.

if this is for printing cheques just listing the digits
is acceptable in my exerience

to_words(1234) => 'one two three four'

this can done using repeated (or nested) replaces.

ɹǝpun uʍop ɯoɹɟ sƃuıʇǝǝɹ⅁

In response to

pgsql-novice by date

Next:From: Mohlomi MoloiDate: 2010-11-08 07:52:32
Subject: Restaring a dumped database
Previous:From: Jasen BettsDate: 2010-11-07 11:14:16
Subject: Re: Describe command alternatives in postgresql

Privacy Policy | About PostgreSQL
Copyright © 1996-2017 The PostgreSQL Global Development Group