Foros Club Delphi

Foros Club Delphi (https://www.clubdelphi.com/foros/index.php)
-   SQL (https://www.clubdelphi.com/foros/forumdisplay.php?f=6)
-   -   Convertir Números a Letras con Centavos (https://www.clubdelphi.com/foros/showthread.php?t=88690)

sam123 16-07-2015 02:39:11

Convertir Números a Letras con Centavos
 
Quien me ayuda, tengo este procedimiento para convertir numeros a Letras, al momento de convertir a letras no convierte los centavos, les agradecere
Código SQL [-]
begin

   if (PVALOR IS NULL) then

   EXIT;

    VVALOR= abs(PVALOR);

   if (vvalor = 0) THEN  VLETRAS =  'CERO';

   else if (VVALOR = 1) then   VLETRAS = 'UN';

   else if (VVALOR = 2) then   VLETRAS = 'DOS';

   else if (VVALOR = 3) then   VLETRAS = 'TRES';

   else if (VVALOR = 4) then   VLETRAS = 'CUATRO';

   else if (VVALOR = 5) then   VLETRAS = 'CINCO';

   else if (VVALOR = 6) then   VLETRAS = 'SEIS';

   else if (VVALOR = 7) then   VLETRAS = 'SIETE';

   else if (VVALOR = 8) then   VLETRAS = 'OCHO';

   else if (VVALOR = 9) then   VLETRAS = 'NUEVE';

   else if (VVALOR = 10) then   VLETRAS = 'DIEZ';

   else if (VVALOR = 11) then   VLETRAS = 'ONCE';

   else if (VVALOR = 12) then   VLETRAS = 'DOCE';

   else if (VVALOR = 13) then   VLETRAS = 'TRECE';

   else if (VVALOR = 14) then   VLETRAS = 'CATORCE';

   else if (VVALOR = 15) then   VLETRAS = 'QUINCE';

   else if (VVALOR < 20) then

   begin

    select rvalor  from sp_numletras(:vvalor - 10) into VELETRAS2;

    VLETRAS = 'DIECI' || VELETRAS2;

   end

   else if (VVALOR = 20) then   VLETRAS = 'VEINTE';

   else if (VVALOR < 30) then

   begin

    select rvalor  from sp_numletras(:pvalor - 20) into VELETRAS2;

    VLETRAS = 'VEINTI' || VELETRAS2;

   end

   else if (VVALOR = 30) then   VLETRAS = 'TREINTA';

   else if (VVALOR = 40) then   VLETRAS = 'CUARENTA';

   else if (VVALOR = 50) then   VLETRAS = 'CINCUENTA';

   else if (VVALOR = 60) then   VLETRAS = 'SESENTA';

   else if (VVALOR = 70) then   VLETRAS = 'SETENTA';

   else if (VVALOR = 80) then   VLETRAS = 'OCHENTA';

   else if (VVALOR = 90) then   VLETRAS = 'NOVENTA';

   else if (VVALOR < 100) then

   begin

   vvlr_entero = TRUNC(VVALOR / 10.2);

   select rvalor  from sp_numletras(:VVLR_ENTERO * 10) into VELETRAS2;

   select rvalor  from sp_numletras(MOD(:VVALOR,10)) into VELETRAS3;

   VLETRAS = VELETRAS2 || ' Y ' || VELETRAS3;

   END

   else if (VVALOR = 100) then   VLETRAS = 'CIEN';

   else if (VVALOR < 200) then

   begin

    select rvalor  from sp_numletras(:vvalor - 100) into VELETRAS2;

    VLETRAS = 'CIENTO  ' || VELETRAS2;

   END

   ELSE  if ((VVALOR = 200) or (VVALOR = 300) or (VVALOR = 400) or (VVALOR = 600) or (VVALOR = 800)) THEN

   begin

   vvlr_entero = TRUNC(VVALOR || 00/100);

    select rvalor  from sp_numletras(:VVLR_ENTERO) into VELETRAS2;

    VLETRAS = VELETRAS2 || ' ' || 'CIENTOS';

   END

   else if (VVALOR = 500) then   VLETRAS = 'QUINIENTOS';

   else if (VVALOR = 700) then   VLETRAS = 'SETECIENTOS';

   else if (VVALOR = 900) then   VLETRAS = 'NOVECIENTOS';

   else if (VVALOR < 1000) then

   begin

   vvlr_entero = TRUNC(VVALOR / 100);

   select rvalor  from sp_numletras(:VVLR_ENTERO * 100) into VELETRAS2;

   select rvalor  from sp_numletras(MOD(:VVALOR,100)) into VELETRAS3;

   VLETRAS = :VELETRAS2 || ' ' || :VELETRAS3;

   END

   else if (VVALOR = 1000) then   VLETRAS = 'MIL';

   else if (VVALOR < 2000) then

   begin

    select rvalor  from sp_numletras(MOD(:vvalor , 1000)) into VELETRAS2;

    VLETRAS = 'MIL  ' || VELETRAS2;

   END

   else if (VVALOR < 1000000) then

   begin

   vvlr_entero = TRUNC(VVALOR / 1000);

   select rvalor  from sp_numletras(:VVLR_ENTERO) into VELETRAS2;

   select rvalor  from sp_numletras(MOD(:VVALOR,1000)) into VELETRAS3;

   VLETRAS = VELETRAS2 || ' MIL ' ||  VELETRAS3;

   END

   else if (VVALOR = 1000000) then   VLETRAS = 'UN MILLON';

   else if (VVALOR < 2000000) then

   begin

    select rvalor  from sp_numletras(MOD(:vvalor , 1000000)) into VELETRAS2;

    VLETRAS = 'UN MILLON  ' || VELETRAS2;

   END

   else if (VVALOR < 1000000000000) then

   begin

   vvlr_entero = TRUNC(VVALOR / 1000000);

   select rvalor  from sp_numletras(:VVLR_ENTERO) into VELETRAS2;

   select rvalor  from sp_numletras(:VVALOR - :VVLR_ENTERO * 1000000) into VELETRAS3;

   VLETRAS = :VELETRAS2 || ' MILLONES ' ||  :VELETRAS3;

   END

   else if (VVALOR = 1000000000000) then   VLETRAS = 'UN BILLON';

   else if (VVALOR < 2000000000000) then

   begin

    vvlr_entero = TRUNC(VVALOR / 1000000000000);

    select rvalor  from sp_numletras(:vvalor - :VVLR_ENTERO * 1000000000000.00) into VELETRAS2;

    VLETRAS = 'UN BILLON  ' || VELETRAS2;

   END

   else

    BEGIN

    vvlr_entero = TRUNC(VVALOR / 1000000000000,00);

    select rvalor  from sp_numletras(:VVLR_ENTERO) into VELETRAS2;

    select rvalor  from sp_numletras(:VVALOR - :VVLR_ENTERO * 1000000000000) into VELETRAS3;

    VLETRAS = VELETRAS2 || ' BILLONES ' ||  VELETRAS3;

    END

   RVALOR = VLETRAS;

  suspend;

end

escafandra 16-07-2015 03:21:11

Puedes pasarte por aquí. Hace unos días, como parte de un reto, desarrollamos tres códigos distintos de escaso tamaño para realizar la conversión de números a texto.


Saludos.

ecfisa 16-07-2015 03:41:01

Hola sam123, bienvenido a Club Delphi :)

Algunos enlaces mas:
Saludos :)

nlsgarcia 16-07-2015 03:42:28

sam123,

¡Bienvenido al Club Delphi! :D

Nelson.

nlsgarcia 16-07-2015 20:01:32

sam123,

Cita:

Empezado por sam123
...Convertir Números a Letras con Centavos...

:rolleyes:

Revisa este código:
Código Delphi [-]
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Edit1: TEdit;
    Memo1: TMemo;
    Button2: TButton;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

// Convierte números a letras de forma recursiva
function NumbersToLetters(Number: Double) : String;
const
  Digit: array[1..9] of String = ('Uno', 'Dos', 'Tres',
                                  'Cuatro', 'Cinco', 'Seis',
                                  'Siete', 'Ocho', 'Nueve');

  Numeral: array[11..19] of String = ('Once', 'Doce', 'Trece',
                                      'Catorce', 'Quince', 'Diez y Seis',
                                      'Diez y Siete', 'Diez y Ocho', 'Diez y Nueve');

  Tenths: array[1..9] of String = ('Diez', 'Veinte', 'Trienta',
                                   'Cuarenta', 'Cincuenta', 'Sesenta',
                                   'Setenta', 'Ochenta', 'Noventa');

  Hundreds : array[1..9] of String = ('Cien ', 'Docientos ', 'Trecientos ',
                                      'Cuatrocientos ', 'Quinientos ', 'Seicientos ',
                                      'Setecientos ', 'Ochocientos ', 'Novecientos ');

  Min = 1;
  Max = 4294967295;

  function RecursiveNumber(N: LongWord) : String;
  begin

    case N of

      1..9:
      begin
         Result := Digit[N];
      end;

      11..19:
      begin
         Result := Numeral[N]
      end;

      10,20..99:
      begin
         if (N Mod 10) = 0 then
            Result := Tenths[N div 10] + RecursiveNumber(N mod 10)
         else
            Result := Tenths[N div 10] + ' y '  + RecursiveNumber(N mod 10);
      end;

      100..999:
      begin
        if (N = 100) or (N >= 200) then Result := Hundreds[N div 100] + RecursiveNumber(N mod 100);
        if (N > 100) and (N < 200) then Result := 'Ciento ' + RecursiveNumber(N mod 100);
      end;

      1000..999999:
      begin
         if (Number >= 1000) and (Number < 2000) then
            Result := 'Un Mil ' + RecursiveNumber(N mod 1000)
         else
            Result := RecursiveNumber(N div 1000) + ' Mil ' + RecursiveNumber(N mod 1000);
      end;

      1000000..999999999:
      begin
         if (Number >= 1000000) and (Number < 2000000) then
            Result := 'Un Millon ' + RecursiveNumber(N mod 1000000)
         else
            Result := RecursiveNumber(N div 1000000) + ' Millones ' + RecursiveNumber(N mod 1000000);
      end;

      1000000000..4294967295:
      begin
         if (Number >= 1000000000) and (Number < 2000000000) then
            Result := 'Un Millardo ' + RecursiveNumber(N mod 1000000)
         else
            Result := RecursiveNumber(N div 1000000000) + ' Millardos ' + RecursiveNumber(N mod 1000000000);
      end;

    end;

  end;

begin
  if (Number >= Min) and (Number <= Max) then
     begin
        Result := RecursiveNumber(Trunc(Number));
        Result := Result +  ' con ' + FormatFloat('00',Frac(Number) * 100) + '/100';
     end
  else
     Result := '-1';
end;

procedure TForm1.Button1Click(Sender: TObject);
var
   Number : Double;
begin
   if TryStrToFloat(Edit1.Text,Number) then
      Memo1.Text := NumbersToLetters(Number)
   else
      MessageDlg('Error en formato de Número',mtError,[mbOK],0);
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
   Edit1.Text := EmptyStr;
   Memo1.Clear;   
end;

end.
El código anterior en Delphi 7 sobre Windows 7 Professional x32, Convierte números a letras por medio de una función recursiva, como se muestra en la siguiente imagen:



Espero sea útil :)

Nelson.

sam123 17-07-2015 01:13:01

Editar este Codigo
 
Alguien me puede editar este codigo que me muestre los centavos al momento que lo convierta a Letras..

saludos

nlsgarcia 17-07-2015 02:19:20

sam123,

Cita:

Empezado por sam123
...este código que me muestre los centavos al momento que lo convierta a Letras...

:rolleyes:

Revisa este código:
Código Delphi [-]
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Edit1: TEdit;
    Memo1: TMemo;
    Button2: TButton;
    GroupBox1: TGroupBox;
    RadioButton1: TRadioButton;
    RadioButton2: TRadioButton;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

// Convierte números a letras de forma recursiva
function NumbersToLetters(Number: Double; Centime : Boolean) : String;
const
  Digit: array[1..9] of String = ('Uno', 'Dos', 'Tres',
                                  'Cuatro', 'Cinco', 'Seis',
                                  'Siete', 'Ocho', 'Nueve');

  Numeral: array[11..19] of String = ('Once', 'Doce', 'Trece',
                                      'Catorce', 'Quince', 'Diez y Seis',
                                      'Diez y Siete', 'Diez y Ocho', 'Diez y Nueve');

  Tenths: array[1..9] of String = ('Diez', 'Veinte', 'Trienta',
                                   'Cuarenta', 'Cincuenta', 'Sesenta',
                                   'Setenta', 'Ochenta', 'Noventa');

  Hundreds : array[1..9] of String = ('Cien ', 'Docientos ', 'Trecientos ',
                                      'Cuatrocientos ', 'Quinientos ', 'Seicientos ',
                                      'Setecientos ', 'Ochocientos ', 'Novecientos ');

  Min = 1;
  Max = 4294967295;

  function RecursiveNumber(N: LongWord): String;
  begin

    case N of

      1..9:
      begin
         Result := Digit[N];
      end;

      11..19:
      begin
         Result := Numeral[N]
      end;

      10,20..99:
      begin
         if (N Mod 10) = 0 then
            Result := Tenths[N div 10] + RecursiveNumber(N mod 10)
         else
            Result := Tenths[N div 10] + ' y '  + RecursiveNumber(N mod 10);
      end;

      100..999:
      begin
        if (N = 100) or (N >= 200) then Result := Hundreds[N div 100] + RecursiveNumber(N mod 100);
        if (N > 100) and (N < 200) then Result := 'Ciento ' + RecursiveNumber(N mod 100);
      end;

      1000..999999:
      begin
         if (Number >= 1000) and (Number < 2000) then
            Result := 'Un Mil ' + RecursiveNumber(N mod 1000)
         else
            Result := RecursiveNumber(N div 1000) + ' Mil ' + RecursiveNumber(N mod 1000);
      end;

      1000000..999999999:
      begin
         if (Number >= 1000000) and (Number < 2000000) then
            Result := 'Un Millon ' + RecursiveNumber(N mod 1000000)
         else
            Result := RecursiveNumber(N div 1000000) + ' Millones ' + RecursiveNumber(N mod 1000000);
      end;

      1000000000..4294967295:
      begin
         if (Number >= 1000000000) and (Number < 2000000000) then
            Result := 'Un Millardo ' + RecursiveNumber(N mod 1000000)
         else
            Result := RecursiveNumber(N div 1000000000) + ' Millardos ' + RecursiveNumber(N mod 1000000000);
      end;

    end;

  end;

var
   F : String;

begin
  if (Number >= Min) and (Number <= Max) then
     begin
        Result := RecursiveNumber(Trunc(Number));
        F := FormatFloat('00',Frac(Number) * 100);
        if Centime then
           Result := Result +  ' con ' + RecursiveNumber(StrToInt(F)) + ' Centimos'
        else
           Result := Result +  ' con ' + F + '/100';
     end
  else
     Result := '-1';
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
   RadioButton1.Checked := True;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
   Number : Double;
begin
   if TryStrToFloat(Edit1.Text,Number) then
   begin
      if RadioButton1.Checked then
         Memo1.Text := NumbersToLetters(Number,True);

      if RadioButton2.Checked then
         Memo1.Text := NumbersToLetters(Number,False)
   end
   else
      MessageDlg('Error en formato de Número',mtError,[mbOK],0);
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
   Edit1.Text := EmptyStr;
   Memo1.Clear;
   RadioButton1.Checked := True;
   Edit1.SetFocus;
end;

end.
El código anterior en Delphi 7 sobre Windows 7 Professional x32 es una variación del presentado en el Msg #5, el cual permite : Convertir números a letras por medio de una función recursiva y escoger si los céntimos son visualizados como letras o fracción, como se muestra en la siguiente imagen:



El código del ejemplo esta disponible en : NumbersToLetters.rar

Espero sea útil :)

Nelson.

sam123 17-07-2015 02:43:52

Muchas gracias por los aporte
 
Talvez no me explique bien, o no logro comprender el cosdigo que me enviastes, que esta excelente pero lo miro funciona coo una aplicacion por separado, yo lo necesito para hacer una factura de ventas, y que automaticamente me lo convierta, si estoy equivocado, sobre el usuo de este codigo me dices como ussarlo, y pegarlo al modulo e facturacion

saludos

nlsgarcia 17-07-2015 02:57:02

sam123,

Cita:

Empezado por sam123
...no logro comprender el código que me enviastes...lo necesito para hacer una factura de ventas...

:rolleyes:

Te comento:

1- El código del Msg #7 sirve como ejemplo del uso de la función NumbersToLetters.

2- Para usar la función NumbersToLetters en cualquier programa, solo debes incluirla en el mismo por medio de una unidad o DLL y ejecutarla con los parámetros requeridos.

Revisa esta información:
Espero sea útil :)

Nelson.


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

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