Aí vai o conteúde de minha unit extenso unit extenso;
interface uses Windows, Messages, SysUtils, Classes, Graphics, Controls, StdCtrls, Forms, Dialogs, Buttons, Menus, ExtCtrls, DB, DBTables, DBCtrls, FileCtrl, Printers, WinProcs, OutLine, IniFiles, Mask; // Funções de uso global // ===================== function TrataGrupo (const S: String): String; function VExtenso (MoedaNoSingular, MoedaNoPlural : String; Valor: Double): String; implementation // Transforma o valor para extenso // =================================================================== // PARAMETROS: // MoedaNoSingular = REAL // MoedaNoPlural = REAIS // Valor = 0.00 function VExtenso(MoedaNoSingular, MoedaNoPlural : String; Valor: Double): String; var Lst : TStringList; I : Integer; Aux : String; Grupo : String; Truncado: Longint; begin Lst := nil; Result := ''; try if Valor = 0.0 then begin Result := 'ZERO ' + MoedaNoPlural; Exit; end; Lst := TStringList.Create; Grupo := ''; Aux := FormatFloat('#,##0.00', Valor); // Separa em grupos // ================ for I := 1 to Length(Aux) do if (Aux[I] = '.') or (Aux[I] = ',') then begin Lst.Add(Grupo); Grupo := ''; end else Grupo := Grupo + Aux[I]; // Inclui o ultimo grupo Lst.Add(Grupo); // Trata os bilhões // ================ I := 0; if Lst.Count > 4 then begin Result := TrataGrupo(Lst[I]); if StrToInt(Lst[I]) = 1 then Result := Result + ' BILHÃO' else Result := Result + ' BILHÕES'; Inc(I); end; // Trata os milhões // ================ if (Lst.Count > 3) then begin if StrToInt(Lst[I]) <> 0 then begin if Length(Result) > 0 then Result := Result + ', '; Result := Result + TrataGrupo(Lst[I]); if StrToInt(Lst[I]) = 1 then Result := Result + ' MILHÃO' else Result := Result + ' MILHÕES'; end; Inc(I); end; // Trata os milhares // ================= if Lst.Count > 2 then begin if StrToInt(Lst[I]) <> 0 then begin if Length(Result) > 0 then Result := Result + ', '; Result := Result + TrataGrupo(Lst[I]); Result := Result + ' MIL'; end; Inc(I); end; // Trata as unidades // ================= if StrToInt(Lst[I]) > 0 then begin if Length(Result) > 0 then Result := Result + ', '; Result := Result + TrataGrupo(Lst[I]); end; Truncado := Trunc(Valor); if Truncado = 1 then Result := Result + ' ' + MoedaNoSingular else if (Truncado = 1000000) or (Truncado = 10000000) or (Truncado = 100000000) or (Truncado = 1000000000) then Result := Result + ' DE ' + MoedaNoPlural else if Truncado <> 0 then Result := Result + ' ' + MoedaNoPlural; Inc(I); // Trata os centavos // ================= if StrToInt(Lst[I]) = 0 then Exit; if Truncado > 0 then Result := Result + ' E '; Result := Result + TrataGrupo(Lst[I]); if StrToInt(Lst[I]) = 1 then Result := Result + ' ' + 'CENTAVO' else Result := Result + ' ' + 'CENTAVOS'; if Truncado = 0 then Result := Result + ' DE ' + MoedaNoSingular; finally // Trata tipo texto Result := AnsiUpperCase(Result); if Lst <> nil then Lst.Free; end; end; // Trata os valores entre 0 e 999 // ============================== function TrataGrupo(const S: String): String; const Num1a19: array [1..19] of String = ( 'UM', 'DOIS', 'TRÊS', 'QUATRO', 'CINCO', 'SEIS', 'SETE', 'OITO', 'NOVE', 'DEZ', 'ONZE', 'DOZE', 'TREZE', 'CATORZE', 'QUINZE', 'DEZESSEIS', 'DEZESSETE', 'DEZOITO', 'DEZENOVE'); Num10a90: array [1..9] of String = ( 'DEZ', 'VINTE', 'TRINTA', 'QUARENTA', 'CINQUENTA', 'SESSENTA', 'SETENTA', 'OITENTA', 'NOVENTA'); Num100a900: array [1..9] of String = ( 'CENTO', 'DUZENTOS', 'TREZENTOS', 'QUATROCENTOS', 'QUINHENTOS', 'SEISCENTOS', 'SETECENTOS', 'OITOCENTOS', 'NOVECENTOS'); var N: Integer; // Trata valores entre 0 e 99 // ========================== function Trata0a99(const S: String; N: Integer): String; begin case N of 0: Result := ''; 1..19: Result := Num1a19[N]; 20..99: begin Result := Num10a90[Ord(S[1]) - Ord('0')]; if S[2] <> '0' then Result := Result + ' E ' + Num1a19[Ord(S[2]) - Ord('0')]; end; end; end; // Trata valores entre 101 e 999 // ============================= function Trata101a999(const S: String; N: Integer): String; var Aux: String[3]; begin Result := Num100a900[Ord(S[1]) - Ord('0')]; if (S[2] <> '0') or (S[3] <> '0') then begin Aux := Copy(S, 2, 2); Result := Result + ' E ' + Trata0a99(Aux, StrToInt(Aux)); end; end; begin N := StrToInt(S); case N of 0..99: Result := Trata0a99(IntToStr(N), N); 100: Result := 'CEM'; 101..999: Result := Trata101a999(S, N); end; end; end. ----- Original Message ----- From: "fpsp2000" <[EMAIL PROTECTED]> To: <delphi-br@yahoogrupos.com.br> Sent: Thursday, February 17, 2005 3:32 PM Subject: [delphi-br] Função para converte valor para extenso Ola! Alguem poderia me passar uma funçao q converta valor para extenso. Desde já agradeço, Fernanda -- <<<<< FAVOR REMOVER ESTA PARTE AO RESPONDER ESTA MENSAGEM >>>>> Para ver as mensagens antigas, acesse: http://br.groups.yahoo.com/group/delphi-br/messages Para falar com o moderador, envie um e-mail para: [EMAIL PROTECTED] ou [EMAIL PROTECTED] Links do Yahoo! Grupos -- No virus found in this incoming message. Checked by AVG Anti-Virus. Version: 7.0.300 / Virus Database: 265.8.8 - Release Date: 14/02/2005 -- No virus found in this outgoing message. Checked by AVG Anti-Virus. Version: 7.0.300 / Virus Database: 265.8.8 - Release Date: 14/02/2005 -- <<<<< FAVOR REMOVER ESTA PARTE AO RESPONDER ESTA MENSAGEM >>>>> Para ver as mensagens antigas, acesse: http://br.groups.yahoo.com/group/delphi-br/messages Para falar com o moderador, envie um e-mail para: [EMAIL PROTECTED] ou [EMAIL PROTECTED] Links do Yahoo! Grupos <*> Para visitar o site do seu grupo na web, acesse: http://br.groups.yahoo.com/group/delphi-br/ <*> Para sair deste grupo, envie um e-mail para: [EMAIL PROTECTED] <*> O uso que você faz do Yahoo! Grupos está sujeito aos: http://br.yahoo.com/info/utos.html