eu uso esta que eu mesmo fiz
CREATE or replace FUNCTION fu_extenso_real(num numeric(20,2)) returns text as $$
-- num -> numero a ser convertido em extenso
begin
return fu_extenso(num,'Real','Reais') ;
end ;
$$ LANGUAGE plpgsql
IMMUTABLE
RETURNS NULL ON NULL INPUT ;
CREATE or replace FUNCTION fu_extenso(num numeric(20,2) , moeda text , moedas
text) returns text as $$
-- num -> numero a ser convertido em extenso
-- moeda -> nome da moeda no singular
-- moedas -> nome da moeda no plural
declare
w_int char(21) ;
x integer ;
v integer ;
w_ret text ;
w_ext text ;
w_apoio text ;
m_cen text[] :=
array['quatrilhão','quatrilhões','trilhão','trilhões','bilhão','bilhões','milhão','milhões','mil','mil']
;
begin
w_ret := '' ;
w_int := to_char(num * 100 , 'fm000000000000000000 00') ;
for x in 1..5 loop
v := cast(substr(w_int,(x-1)*3 + 1,3) as integer) ;
if v > 0 then
if v > 1 then
w_ext := m_cen[(x-1)*2+2] ;
else
w_ext := m_cen[(x-1)*2+1] ;
end if ;
w_ret := w_ret || fu_extenso_blk(substr(w_int,(x-1)*3 + 1,3)) || ' '
|| w_ext ||', ' ;
end if ;
end loop ;
v := cast(substr(w_int,16,3) as integer) ;
if v > 0 then
if v > 1 then
w_ext := moedas ;
else
if w_ret = '' then
w_ext := moeda ;
else
w_ext := moedas ;
end if ;
end if ;
w_apoio := fu_extenso_blk(substr(w_int,16,3)) || ' ' || w_ext ;
if w_ret = '' then
w_ret := w_apoio ;
else
if v > 100 then
if w_ret = '' then
w_ret := w_apoio ;
else
w_ret := w_ret || w_apoio ;
end if ;
else
w_ret := btrim(w_ret,', ') || ' e ' || w_apoio ;
end if ;
end if ;
else
if w_ret <> '' then
if substr(w_int,13,6) = '000000' then
w_ret := btrim(w_ret,', ') || ' de ' || moedas ;
else
w_ret := btrim(w_ret,', ') || ' ' || moedas ;
end if ;
end if ;
end if ;
v := cast(substr(w_int,20,2) as integer) ;
if v > 0 then
if v > 1 then
w_ext := 'centavos' ;
else
w_ext := 'centavo' ;
end if ;
w_apoio := fu_extenso_blk('0'||substr(w_int,20,2)) || ' ' || w_ext ;
if w_ret = '' then
w_ret := w_apoio || ' de ' || moeda;
else
w_ret := w_ret || ' e ' || w_apoio ;
end if ;
end if ;
return w_ret ;
end ;
$$ LANGUAGE plpgsql
IMMUTABLE
RETURNS NULL ON NULL INPUT ;
CREATE or replace FUNCTION fu_extenso_blk(num char(3)) returns text as $$
declare
w_cen integer ;
w_dez integer ;
w_dez2 integer ;
w_uni integer ;
w_tcen text ;
w_tdez text ;
w_tuni text ;
w_ext text ;
m_cen text[] :=
array['','cento','duzentos','trezentos','quatrocentos','quinhentos','seiscentos','setecentos','oitocentos','novecentos'];
m_dez text[] :=
array['','dez','vinte','trinta','quarenta','cinquenta','sessenta','setenta','oitenta','noventa']
;
m_uni text[] :=
array['','um','dois','três','quatro','cinco','seis','sete','oito','nove','dez','onze','doze','treze','quatorze','quinze','dezesseis','dezessete','dezoito','dezenove']
;
begin
w_cen := cast(substr(num,1,1) as integer) ;
w_dez := cast(substr(num,2,1) as integer) ;
w_dez2 := cast(substr(num,2,2) as integer) ;
w_uni := cast(substr(num,3,1) as integer) ;
if w_cen = 1 and w_dez2 = 0 then
w_tcen := 'Cem' ;
w_tdez := '' ;
w_tuni := '' ;
else
if w_dez2 < 20 then
w_tcen := m_cen[w_cen + 1] ;
w_tdez := m_uni[w_dez2 + 1] ;
w_tuni := '' ;
else
w_tcen := m_cen[w_cen + 1] ;
w_tdez := m_dez[w_dez + 1] ;
w_tuni := m_uni[w_uni + 1] ;
end if ;
end if ;
w_ext := w_tcen ;
if w_tdez <> '' then
if w_ext = '' then
w_ext := w_tdez ;
else
w_ext := w_ext || ' e ' || w_tdez ;
end if ;
end if ;
if w_tuni <> '' then
if w_ext = '' then
w_ext := w_tuni ;
else
w_ext := w_ext || ' e ' || w_tuni ;
end if ;
end if ;
return w_ext ;
end ;
$$ LANGUAGE plpgsql
IMMUTABLE
RETURNS NULL ON NULL INPUT ;
----- Original Message -----
From: flavio cardoso
To: Comunidade PostgreSQL Brasileira
Sent: Thursday, December 06, 2007 1:16 PM
Subject: Re: [pgbr-geral] Numero Extenso
também quero !!! manda pra mim também !!
Em 06/12/07, Roberto Baselio Lopes <[EMAIL PROTECTED]> escreveu:
Diego, se possivel mande pra mim tambem
2007/12/6, Diego Altheman dos Santos < [EMAIL PROTECTED]>:
Julio ,
Tenho sim. Assim que chegar em casa a noite eu envio pro teu email
On Thu, 6 Dec 2007 09:07:58 -0300, Julio Cesar Merenda Catardo wrote
> Pessoal, bom dia
>
> alguém possui uma função para gerar o extenso de um numero ????
--
www.phpinfo.eti.br
Diego Altheman
_______________________________________________
pgbr-geral mailing list
[email protected]
https://listas.postgresql.org.br/cgi-bin/mailman/listinfo/pgbr-geral
--
Roberto Baselio Lopes
e-mail / Google Talk: [EMAIL PROTECTED]
msn: [EMAIL PROTECTED]
Curriculo: http://www2.curriculum.com.br/ucn/rbaselio
_______________________________________________
pgbr-geral mailing list
[email protected]
https://listas.postgresql.org.br/cgi-bin/mailman/listinfo/pgbr-geral
--
O temor do Senhor é o princípio do conhecimento; mas os insensatos desprezam
a sabedoria e a instrução. Pv 1;7
Flávio Cardoso
------------------------------------------------------------------------------
_______________________________________________
pgbr-geral mailing list
[email protected]
https://listas.postgresql.org.br/cgi-bin/mailman/listinfo/pgbr-geral
_______________________________________________
pgbr-geral mailing list
[email protected]
https://listas.postgresql.org.br/cgi-bin/mailman/listinfo/pgbr-geral