Foros Club Delphi

Foros Club Delphi (https://www.clubdelphi.com/foros/index.php)
-   Varios (https://www.clubdelphi.com/foros/forumdisplay.php?f=11)
-   -   Convertir Numeros en Letras (https://www.clubdelphi.com/foros/showthread.php?t=79172)

hondaalberto 13-06-2012 15:59:30

Convertir Numeros en Letras
 
Buenos dias Amigos quiero saber como puedo probar esta función en delphi para ver su funcionamiento... o sea que componentes debo bajar a un formulario para ver el resultado que devuelve la misma y en que evento debo llamarla y como.

Código Delphi [-]
unit FuncionNumLetras;

interface
 Uses SysUtils;

 Function CantidadALetra(curCantidad: Currency; MonedaNacional: Boolean): String;

implementation
 Var
  i: integer;
  Cantidad, Centavos: Currency;
  BloqueCero, NumeroBloques, Digito: Byte;
  PrimerDigito, SegundoDigito, TercerDigito: Byte;
  Resultado, Temp, Bloque: String;
  Unidades: Array[0..28] of String;
  Decenas: Array[0..8] of String;
  Centenas: Array[0..8] of String;

Function CantidadALetra(curCantidad: Currency; MonedaNacional: Boolean): String;
begin

Unidades[0] := 'UN'; Unidades[1] := 'DOS'; Unidades[2] := 'TRES'; Unidades[3] := 'CUATRO';
Unidades[4] := 'CINCO'; Unidades[5] := 'SEIS'; Unidades[6] := 'SIETE'; Unidades[7] := 'OCHO';
Unidades[8] := 'NUEVE'; Unidades[9] := 'DIEZ'; Unidades[10] := 'ONCE'; Unidades[11] := 'DOCE';
Unidades[12] := 'TRECE'; Unidades[13] := 'CATORCE'; Unidades[14] := 'QUINCE'; Unidades[15] := 'DIESISEIS';
Unidades[16] := 'DIESISIETE'; Unidades[17] := 'DIESIOCHO'; Unidades[18] := 'DIESINUEVE';
Unidades[19] := 'VEINTE'; Unidades[20] := 'VEINTIUNO'; Unidades[21] := 'VEINTIDOS';
Unidades[22] := 'VEINTITRES'; Unidades[23] := 'VEINTICUATRO'; Unidades[24] := 'VEINTICINCO';
Unidades[25] := 'VEINTISEIS'; Unidades[26] := 'VEINTISIETE'; Unidades[27] := 'VEINTIOCHO'; Unidades[28] := 'VEINTINUEVE';

Decenas[0] := 'DIEZ'; Decenas[1] := 'VEINTE'; Decenas[2] := 'TREINTA'; Decenas[3] := 'CUARENTA';
Decenas[4] := 'CINCUENTA'; Decenas[5] := 'SESENTA'; Decenas[6] := 'SETENTA'; Decenas[7] := 'OCHENTA'; Decenas[8] := 'NOVENTA';

Centenas[0] := 'CIENTO'; Centenas[1] := 'DOSCIENTOS'; Centenas[2] := 'TRESCIENTOS'; Centenas[3] := 'CUATROCIENTOS';
Centenas[4] := 'QUINIENTOS'; Centenas[5] := 'SEISCIENTOS'; Centenas[6] := 'SETECIENTOS'; Centenas[7] := 'OCHOCIENTOS'; Centenas[8] := 'NOVECIENTOS';

curCantidad := (curCantidad);
Cantidad := Int(curCantidad);
Centavos := (curCantidad - Cantidad) * 100;
NumeroBloques := 1;
Repeat
 PrimerDigito := 0;
 SegundoDigito := 0;
 TercerDigito := 0;
 Bloque := '';
 BloqueCero := 0;
 For i := 1 To 3 do begin
  Digito := Round(Cantidad) Mod 10;
  If Digito <> 0 Then begin
   Case i of
    1: begin
     Bloque := ' ' + Unidades[Digito - 1];
     PrimerDigito := Digito;
    end; //case 1
    2: begin
      If Digito <= 2 Then begin
       Bloque := ' ' + Unidades[(Digito * 10 + PrimerDigito - 1)];
      end Else begin
       If PrimerDigito <> 0 then
        Temp := ' Y' else Temp := '';
       Bloque := ' ' + Decenas[Digito - 1] + Temp + Bloque;
      End; //if
      SegundoDigito := Digito;
     end; //case 2
    3: begin
     If (Digito = 1) and (PrimerDigito = 0) and (SegundoDigito = 0) then
      Temp := 'CIEN' else Temp := Centenas[Digito-1];
     Bloque := ' ' + Temp + Bloque;
     TercerDigito := Digito;
     end; //case 3
    End; //case
  end Else begin
  BloqueCero := BloqueCero + 1;
  End; // If Digito <>0
  Cantidad := Int(Cantidad / 10);
  If Cantidad = 0 Then begin
   Break;
  End; // If Cantidad=0
 end; //for
 Case NumeroBloques of
  1:
   Resultado := Bloque;
  2: begin
   if BloqueCero = 3 then
    Temp := '' else Temp := ' MIL';
   Resultado := Bloque + Temp + Resultado;
   end; //case 2
  3: begin
   If (PrimerDigito = 1) and (SegundoDigito = 0) and (TercerDigito = 0) then
    Temp := ' MILLON' else Temp := ' MILLONES';
   Resultado := Bloque + Temp + Resultado;
   end; //case 3
 End; //case
 NumeroBloques := NumeroBloques + 1;
Until Cantidad = 0; //repeat
If MonedaNacional Then begin
 If curCantidad > 1 then
  Temp := ' PESOS ***' else Temp := ' PESO ***';
 CantidadALetra := 'SON: *** ' + Resultado + Temp + FormatFloat('00',Centavos) + '/100 M.N. ';
end Else begin
 If curCantidad > 1 then
  Temp := ' DLLS ***' else Temp := ' DOLAR ***';
 CantidadALetra := 'SON: *** ' + Resultado + Temp + FormatFloat('00',Centavos) + '/100';
End; // If MonedaNacional
End;

end.


Muchas Gracias por anticipado y mil disculpas, pero lo que pasa es que soy muy novato.

Chris 13-06-2012 16:10:29

Puedes colocar un botón en cualquier formulario o en un formulario en blanco. En el evento OnClick del botón agrega el siguiente código:
Código Delphi [-]
procedure TForm1.Button1OnClick(Sender: TObject);
begin
    ShowMessage(CantidadALetra(8.50, True);
    ShowMessage(CantidadALetra(15.50, True);
    ShowMessage(CantidadALetra(36.50, True);
end;

Ése sería la forma más rápida de probar el resultado de una función. Un método más preciso es ir haciendo una prueba cada intervalo de números e ir agregando el resultado a un memo. Así puedes ver visualmente si en algunos escenarios la función no devuelva el resultado esperado. Por ejemplo:
Código Delphi [-]
procedure ProbarFuncion();
var
    I: Integer;
    test_value: Currency
begin
    for I := 0 to 1000 do
    begin
        if (I mod 4)  0 then
        begin
            test_value := I + .25;
            Memo1.Lines.add(Format('%f.2 => %s', 
                            [test_value, CantidadALetra(test_value, True)]));
        end;
    end;
end;

El último código lo he escrito sin verificarlo. Puede que tenga algunos errores. Pero a como dice el Chapulín: La idea es ésa :)

Saludos!


La franja horaria es GMT +2. Ahora son las 19:34:09.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Traducción al castellano por el equipo de moderadores del Club Delphi