Function NumAPalabra(Cantidad:real):String;
const
{Declaramos las unidades del 1 al 29}
Unidades: array [1..29] of string[15]= ('UN','DOS','TRES','CUATRO','CINCO','SEIS','SIETE','OCHO','NUEVE','DIEZ',
'ONCE','DOCE','TRECE','CATORCE','QUINCE','DIECISEIS','DIECISIETE','DIECIOCHO','DIECINUEVE','VEINTE',
'VEINTIUN','VEINTIDOS','VEINTITRES','VEINTICUATRO','VEINTICINCO','VEINTISEIS','VEINTISIETE','VEINTIOCHO','VEINTINUEVE');
{declaramos las decenas del 10 al 90}
Decenas : array [1..9] of String[15] = ('DIEZ','VEINTE','TREINTA','CUARENTA','CINCUENTA','SESENTA','SETENTA','OCHENTA','NOVENTA');
{Declaramos las centenas del 100 al 900}
Centenas :Array [1..9] of string[15] = ('CIENTO','DOSCIENTOS','TRESCIENTOS','CUATROCIENTOS','QUINIENTOS','SEISCIENTOS','SETECIENTOS','OCHOCIENTOS','NOVECIENTOS' );
var {declaramos las variables usadas en la funcion}
CantidadReal :Real;
CantidadEntera ,TmpCantidadentera :longInt;
Centavos :REAL;
TextoCentavos :String;
TnDigito ,TnPrimerDigito ,
TnSegundoDigito ,TnTercerDigito :integer;
BloqueTexto :String;
TnNumeroBloques :Byte;
TnBloqueCero :integer;
Contador :integer;
Begin
{Inicializamos las variables}
centavos := 0;
CantidadEntera := Trunc(Cantidad);
TmpCantidadentera:= CantidadEntera;
Centavos := round(((CantidadEntera-Cantidad) * 100 )* -1);
TnDigito := 0;
TnNumeroBloques := 1;
TnPrimerDigito := 0;
TnSegundoDigito := 0;
TnTercerDigito := 0;
BloqueTexto := '';
TnBloqueCero := 1;
Repeat
For Contador:= 1 To 3 do {ciclo para determinar unidades,decenas ,centenas}
Begin
TnDigito:=TmpCantidadEntera Mod 10;{hacemos el recorrido por cada digito}
If TnDigito <> 0 Then{si no es cero buscamos en los arreglos constantes}
Begin
Case Contador of
1:Begin {si son unidades}
BloqueTexto := ' '+ Unidades[TnDigito];
TnPrimerDigito := TnDigito ;
End;
2:Begin {si son decenas}
If TnDigito <= 2 Then
BloqueTexto := ' ' + Unidades[(TnDigito * 10) + TnPrimerDigito ]+ ' '
Else
Begin
If TnPrimerDigito <> 0 then
BloqueTexto := ' '+ Decenas[TnDigito]+ ' Y'+BloqueTexto
Else
If TnDigito <> 0 then
BloqueTexto := ' '+ Decenas[TnDigito]
Else
BloqueTexto:= BloqueTexto
End;
TnSegundoDigito := TnDigito;
End;
3:Begin {si son centenas}
IF (TnDigito = 1) and (TnPrimerDigito = 0) and (TnSegundoDigito = 0) then
BloqueTexto:=' CIEN '+BloqueTexto
Else
BloqueTexto:= Centenas[TnDigito] + BloqueTexto;
TnTercerDigito := TnDigito;
End;
End;{case}
TmpCantidadentera:=trunc(TmpCantidadentera/10);{dividimos entre 10 para recorrer la cifra}
End
Else
Begin
TnBloqueCero := TnBloqueCero + 1 ;{validamos los bloques para determinar miles y millones}
TmpCantidadentera:=trunc(TmpCantidadentera/10);{dividimos entre 10 para recorrer la cifra}
If TmpCantidadEntera = 0 Then {rompiendo el ciclo forzozamente}
Contador:=3;
End;
End;{for}
Case TnNumeroBloques of {validando miles y millones}
1: Begin {validando cientos}
NumAPalabra := BloqueTexto;
End;
2 :Begin {validando miles}
If TnBloqueCero < 2 then
NumAPalabra:=BloqueTexto+NumAPalabra
Else
begin
If (TnPrimerDigito = 0) and (TnSegundoDigito = 0) and (TnTercerDigito = 0) then
NumAPalabra:=BloqueTexto+NumAPalabra
Else
NumAPalabra:=BloqueTexto+' MIL '+NumAPalabra;
End;
End;
3 :Begin {validando millones}
If (TnPrimerDigito = 1) and (TnSegundoDigito = 0) and (TnTercerDigito = 0) then
NumAPalabra:= BloqueTexto + ' MILLON '+ NumAPalabra
Else
NumAPalabra:= BloqueTexto + ' MILLONES '+ NumAPalabra;
End;
End;{case}
TnNumeroBloques := TnNumeroBloques + 1 ; {incrementando el bloque}
{inicializando variables para las siguientes pasadas en caso de haber miles y millones}
TnDigito := 0;
TnPrimerDigito := 0;
TnSegundoDigito := 0;
TnTercerDigito := 0;
BloqueTexto := '';
Until TmpCantidadEntera = 0 ;{validar hasta que que sea 0}
Str(Centavos:2:0,TextoCentavos);{convirtiendo los centavos a texto}
If CantidadEntera > 1 then {si la cantidad es mayor que 1}
NumAPalabra:= 'SON: ('+ NumAPalabra +' PESOS '+TextoCentavos+'/100 M.N)'
Else {en caso que sea 1}
NumAPalabra:= 'SON: ('+ NumAPalabra +' PESO '+TextoCentavos+'/100 M.N)'
End;