Number Conversion Function

Started by Abdul Rahmanabout 17 years ago10 messagesgeneral
Jump to latest
#1Abdul Rahman
abr_ora@yahoo.com

Hi all,

Can any body help me in converting numeric values into words via postgres function: i.e. 313 to THREE HUNDRED THREE

Regards,
Abdul Rehman.

#2Tino Wildenhain
tino@living-examples.com
In reply to: Abdul Rahman (#1)
Re: Number Conversion Function

Abdul Rehman wrote:

Hi all,

Can any body help me in converting numeric values into words via
postgres function: i.e. 313 to THREE HUNDRED THREE

I would not recommend to do this within the database. Thats typical
a job for your presentation layer.

Regards
Tino

#3A. Kretschmer
andreas.kretschmer@schollglas.com
In reply to: Abdul Rahman (#1)
Re: Number Conversion Function

In response to Abdul Rehman :

Hi all,

Can any body help me in converting numeric values into words via postgres
function: i.e. 313 to THREE HUNDRED THREE

You can use the PERL-Module from
http://search.cpan.org/~sburke/Lingua-EN-Numbers-1.01/lib/Lingua/EN/Numbers.pm
and plperlU

Andreas
--
Andreas Kretschmer
Kontakt: Heynitz: 035242/47150, D1: 0160/7141639 (mehr: -> Header)
GnuPG-ID: 0x3FFF606C, privat 0x7F4584DA http://wwwkeys.de.pgp.net

#4Tom Lane
tgl@sss.pgh.pa.us
In reply to: Tino Wildenhain (#2)
Re: Number Conversion Function

Tino Wildenhain <tino@living-examples.com> writes:

Abdul Rehman wrote:

Can any body help me in converting numeric values into words via
postgres function: i.e. 313 to THREE HUNDRED THREE

I would not recommend to do this within the database. Thats typical
a job for your presentation layer.

... but having said that, I think the "money" datatype has a function
for this. Whether that's of any use to you I dunno; money is pretty
restrictive about what it can handle.

regards, tom lane

#5Laurenz Albe
laurenz.albe@cybertec.at
In reply to: Abdul Rahman (#1)
Re: Number Conversion Function

Abdul Rehman wrote:

Can any body help me in converting numeric values into words
via postgres function: i.e. 313 to THREE HUNDRED THREE

Here is a little PL/pgSQL function that should do what you
want for numbers < 1000000000000.
It should be easy to extend if you switch to bigint and decide
if you want British or American English.

I played with it, but it may still contain bugs.

Yours,
Laurenz Albe

CREATE OR REPLACE FUNCTION to_text(integer) RETURNS text LANGUAGE sql IMMUTABLE STRICT AS
$$SELECT CASE WHEN $1<1 THEN NULL
WHEN $1=1 THEN 'ONE'
WHEN $1=2 THEN 'TWO'
WHEN $1=3 THEN 'THREE'
WHEN $1=4 THEN 'FOUR'
WHEN $1=5 THEN 'FIVE'
WHEN $1=6 THEN 'SIX'
WHEN $1=7 THEN 'SEVEN'
WHEN $1=8 THEN 'EIGHT'
WHEN $1=9 THEN 'NINE'
WHEN $1=10 THEN 'TEN'
WHEN $1=11 THEN 'ELEVEN'
WHEN $1=12 THEN 'TWELVE'
WHEN $1=13 THEN 'THIRTEEN'
WHEN $1=14 THEN 'FOURTEEN'
WHEN $1=15 THEN 'FIFTEEN'
WHEN $1=16 THEN 'SIXTEEN'
WHEN $1=17 THEN 'SEVENTEEN'
WHEN $1=18 THEN 'EIGHTEEN'
WHEN $1=19 THEN 'NINETEEN'
WHEN $1<100 THEN CASE
WHEN $1/10=2 THEN 'TWENTY' || COALESCE(' ' || to_text($1%10), '')
WHEN $1/10=3 THEN 'THIRTY' || COALESCE(' ' || to_text($1%10), '')
WHEN $1/10=4 THEN 'FOURTY' || COALESCE(' ' || to_text($1%10), '')
WHEN $1/10=5 THEN 'FIFTY' || COALESCE(' ' || to_text($1%10), '')
WHEN $1/10=6 THEN 'SIXTY' || COALESCE(' ' || to_text($1%10), '')
WHEN $1/10=7 THEN 'SEVENTY' || COALESCE(' ' || to_text($1%10), '')
WHEN $1/10=8 THEN 'EIGHTY' || COALESCE(' ' || to_text($1%10), '')
WHEN $1/10=9 THEN 'NINETY' || COALESCE(' ' || to_text($1%10), '')
END
WHEN $1<1000
THEN to_text($1/100) || ' HUNDRED' ||
COALESCE(' AND ' || to_text($1%100), '')
WHEN $1<1000000
THEN to_text($1/1000) || ' THOUSAND' ||
CASE WHEN $1%1000 < 100
THEN COALESCE(' AND ' || to_text($1%1000), '')
ELSE COALESCE(' ' || to_text($1%1000), '')
END
WHEN $1<1000000000
THEN to_text($1/1000000) || ' MILLION' ||
CASE WHEN $1%1000000 < 100
THEN COALESCE(' AND ' || to_text($1%1000000), '')
ELSE COALESCE(' ' || to_text($1%1000000), '')
END
END$$;

#6Alvaro Herrera
alvherre@2ndquadrant.com
In reply to: Laurenz Albe (#5)
Re: Number Conversion Function

Albe Laurenz wrote:

Abdul Rehman wrote:

Can any body help me in converting numeric values into words
via postgres function: i.e. 313 to THREE HUNDRED THREE

Here is a little PL/pgSQL function that should do what you
want for numbers < 1000000000000.
It should be easy to extend if you switch to bigint and decide
if you want British or American English.

This is something that would be probably useful to have in
http://wiki.postgresql.org/wiki/Snippets
We have a function there to generate the numbers in spanish ...

--
Alvaro Herrera http://www.CommandPrompt.com/
PostgreSQL Replication, Consulting, Custom Development, 24x7 support

#7Justin
justin@emproshunts.com
In reply to: Tom Lane (#4)
Re: Number Conversion Function

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta content="text/html;charset=ISO-8859-1" http-equiv="Content-Type">
</head>
<body bgcolor="#ffffff" text="#000000">
<br>
<br>
Tom Lane wrote:
<blockquote cite="mid:27615.1239027099@sss.pgh.pa.us" type="cite">
<pre wrap="">Tino Wildenhain <a class="moz-txt-link-rfc2396E" href="mailto:tino@living-examples.com">&lt;tino@living-examples.com&gt;</a> writes:
</pre>
<blockquote type="cite">
<pre wrap="">I would not recommend to do this within the database. Thats typical
a job for your presentation layer.
</pre>
</blockquote>
<pre wrap=""><!---->
... but having said that, I think the "money" datatype has a function
for this. Whether that's of any use to you I dunno; money is pretty
restrictive about what it can handle.

regards, tom lane
</pre>
</blockquote>
<br>
I disagree the database is the wrong place, there are cases it makes
sense.<br>
<br>
I have looked for what Tom talks about for the money type i can't find
any thing??&nbsp; so I wrote a function primary purpose is used with checks
but with a little modification will work for anyone one and has no
practical limitation .&nbsp;&nbsp; It will work to Quintillion. <br>
<br>
CREATE OR REPLACE FUNCTION spellNumericValue( pValue numeric)<br>
&nbsp; RETURNS text AS<br>
$BODY$<br>
DECLARE<br>
&nbsp; _dollar bigint = trunc(pValue)::text;<br>
&nbsp; _cents int = ((pValue - trunc(pValue))*100)::int;<br>
&nbsp; _spelledAmount text = '' ;<br>
&nbsp; _brokenOut int[] ;<br>
&nbsp; _pos integer = 0;<br>
&nbsp; _word text ;<br>
BEGIN<br>
&nbsp;&nbsp;&nbsp; <br>
&nbsp;&nbsp;&nbsp; --lets breakout the number into hundreds into a array<br>
&nbsp;&nbsp;&nbsp; WHILE _dollar &gt; 0 loop<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; _brokenOut = array_append(_brokenOut, (_dollar%1000)::int);<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; _dollar = trunc(_dollar/1000);<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; _pos = _pos + 1;<br>
&nbsp;&nbsp;&nbsp; End Loop;<br>
<br>
&nbsp;&nbsp;&nbsp; --this works on numbers between 1 to 999 transforming into english
words. then goes to the <br>
&nbsp;&nbsp;&nbsp; --next set of numbers in the array working backwards as the array
was loaded backwards<br>
&nbsp;&nbsp;&nbsp; --Meaning the highest value is in the last element of the array
_brokenOut<br>
&nbsp;&nbsp;&nbsp; --This also assumes words thousands millions, billions... occurs
every 10^3 .<br>
&nbsp;&nbsp;&nbsp; while _pos &gt; 0 loop<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; if _brokenOut[_pos] &gt;99 then<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; SELECT CASE <br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; WHEN _brokenOut[_pos] &gt; 899 THEN 'Nine Hundred '<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; WHEN _brokenOut[_pos] &gt; 799 THEN 'Eight Hundred '<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; WHEN _brokenOut[_pos] &gt; 699 THEN 'Seven Hundred '<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; WHEN _brokenOut[_pos] &gt; 599 THEN 'Six Hundred ' <br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; WHEN _brokenOut[_pos] &gt; 499 THEN 'Five Hundred '<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; WHEN _brokenOut[_pos] &gt; 399 THEN 'Four Hundred '<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; WHEN _brokenOut[_pos] &gt; 299 THEN 'Three Hundred '<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; WHEN _brokenOut[_pos] &gt; 199 THEN 'Two Hundred '<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; WHEN _brokenOut[_pos] &gt; 99 THEN 'One Hundred '<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; else ''<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; end<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; into _word;<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; _spelledAmount = _spelledAmount || _word ;<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; end if;<br>
<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; Select Case<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; WHEN _brokenOut[_pos]%100 = 10 THEN 'Ten '<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; WHEN _brokenOut[_pos]%100 = 11 THEN 'Eleve '<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; WHEN _brokenOut[_pos]%100 = 12 THEN 'Twelve '<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; WHEN _brokenOut[_pos]%100 = 13 THEN 'Thirteen '<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; WHEN _brokenOut[_pos]%100 = 14 THEN 'Fourteen '<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; WHEN _brokenOut[_pos]%100 = 15 THEN 'Fifteen '<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; WHEN _brokenOut[_pos]%100 = 16 THEN 'Sixteen '<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; WHEN _brokenOut[_pos]%100 = 17 THEN 'Seventeen '<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; WHEN _brokenOut[_pos]%100 = 18 THEN 'Eighteen'<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; WHEN _brokenOut[_pos]%100 = 19 THEN 'Nineteen '<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; WHEN _brokenOut[_pos]/10%10=2 THEN 'Twenty ' <br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; WHEN _brokenOut[_pos]/10%10=3 THEN 'Thirty ' <br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; WHEN _brokenOut[_pos]/10%10=4 THEN 'Fourty ' <br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; WHEN _brokenOut[_pos]/10%10=5 THEN 'Fifty ' <br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; WHEN _brokenOut[_pos]/10%10=6 THEN 'Sixty ' <br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; WHEN _brokenOut[_pos]/10%10=7 THEN 'Seventy ' <br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; WHEN _brokenOut[_pos]/10%10=8 THEN 'Eighty ' <br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; WHEN _brokenOut[_pos]/10%10=9 THEN 'Ninety&nbsp; ' <br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; ELSE ''<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; End<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; into _word;<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; _spelledAmount = _spelledAmount || _word;<br>
<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; if _brokenOut[_pos]%100 &lt; 10 or _brokenOut[_pos]%100 &gt; 20
then<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; SELECT CASE <br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; WHEN _brokenOut[_pos]%10 = 1 THEN 'One '<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; WHEN _brokenOut[_pos]%10 = 2 THEN 'Two'<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; WHEN _brokenOut[_pos]%10 = 3 THEN 'Three '<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; WHEN _brokenOut[_pos]%10 = 4 THEN 'Four '<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; WHEN _brokenOut[_pos]%10 = 5 THEN 'Five '<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; WHEN _brokenOut[_pos]%10 = 6 THEN 'Six '<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; WHEN _brokenOut[_pos]%10 = 7 THEN 'Seven '<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; WHEN _brokenOut[_pos]%10 = 8 THEN 'Eight '<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; WHEN _brokenOut[_pos]%10 = 9 THEN 'Nine '<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; ELSE ''<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; end<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; into _word;<br>
&nbsp;&nbsp;&nbsp; <br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; _spelledAmount = _spelledAmount || _word;<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; end if ;<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; If _pos = 2 then<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; _spelledAmount = _spelledAmount || 'Thousand ';<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; elsif _pos = 3&nbsp; then<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; _spelledAmount = _spelledAmount || 'Million';<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; elsif _pos = 4&nbsp; then<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; _spelledAmount = _spelledAmount || 'Billion ';<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; elsif _pos = 5 then<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; _spelledAmount = _spelledAmount || 'Trillion ';<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; elsif _pos = 6 then<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; _spelledAmount = _spelledAmount || 'Quadrillion ';<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; elsif _pos = 7 then<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; _spelledAmount = _spelledAmount || 'Quintillion ';<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; else <br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; _spelledAmount = _spelledAmount || '';<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; end if;<br>
<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; _pos = _pos-1;<br>
&nbsp;&nbsp;&nbsp; end loop;<br>
<br>
&nbsp;&nbsp;&nbsp; if _cents = 0 then<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; _spelledAmount = _spelledAmount || ' and Zero cents';<br>
&nbsp;&nbsp;&nbsp; else<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; _spelledAmount = _spelledAmount || 'and ' || _cents::text ||
'/100 cents';<br>
&nbsp;&nbsp;&nbsp; end if ;<br>
&nbsp;&nbsp;&nbsp; return _SpelledAmount;<br>
&nbsp;&nbsp;&nbsp; <br>
END;<br>
$BODY$<br>
&nbsp; LANGUAGE 'plpgsql' VOLATILE<br>
<br>
<br>
</body>
</html>

#8Tino Wildenhain
tino@wildenhain.de
In reply to: Justin (#7)
Re: Number Conversion Function

justin wrote:

Tom Lane wrote:

Tino Wildenhain <tino@living-examples.com> writes:

I would not recommend to do this within the database. Thats typical
a job for your presentation layer.

... but having said that, I think the "money" datatype has a function
for this. Whether that's of any use to you I dunno; money is pretty
restrictive about what it can handle.

regards, tom lane

I disagree the database is the wrong place, there are cases it makes sense.

Which cases would that be?

Regards
Tino

#9Justin
justin@emproshunts.com
In reply to: Tino Wildenhain (#8)
Re: Number Conversion Function

Tino Wildenhain wrote:

justin wrote:

I disagree the database is the wrong place, there are cases it makes
sense.

Which cases would that be?

Regards
Tino

Report engines that don't have this ability . I use for check writing.

To come think, I don't know of UI framework or report engine that has
this ability.

#10Merlin Moncure
mmoncure@gmail.com
In reply to: Tino Wildenhain (#8)
Re: Number Conversion Function

On Mon, Apr 13, 2009 at 2:40 PM, Tino Wildenhain <tino@wildenhain.de> wrote:

justin wrote:

Tom Lane wrote:

Tino Wildenhain <tino@living-examples.com> writes:

I would not recommend to do this within the database. Thats typical
a job for your presentation layer.

... but having said that, I think the "money" datatype has a function
for this.  Whether that's of any use to you I dunno; money is pretty
restrictive about what it can handle.

                       regards, tom lane

I disagree the database is the wrong place, there are cases it makes
sense.

Which cases would that be?

What if you had several different 'presentation layers' interfacing
with the database? Say, a internal web app, some reporting programs
used by sales, dept, the old delphi program that no one ever bothered
to rewrite, etc. Do you really want to implement the formatting
function in all those places?

as a general rule, the more i do in the database, the easier things
are for me in the long run.

merlin