Código:
Function Expresion(XNumero: Variant): String;
const
Unidades: array[1..9] of String = ('Un ','Dos ','Tres ','Cuatro ','Cinco ','Seis ','Siete ','Ocho ','Nueve ');
Decimos: array[0..9] of String = ('Diez ','Once ','Doce ','Trece ','Catorce ','Quince ','Dieciseis ','Diecisiete ','Dieciocho ','Diecinueve ');
Decenas: array[1..9] of String = ('Diez ','Veinte ','Treinta ','Cuarenta ','Cincuenta ','Sesenta ','Setenta ','Ochenta ','Noventa ');
Centenas: array[1..9] of String = ('Cien ','Doscientos ','Trescientos ','Cuatrocientos ','Quinientos ','Seiscientos ','Setecientos ','Ochocientos ','Novecientos ');
var
cNumero: String;
nInd, nDigito, nPos: Integer;
LDec: Boolean;
begin
Result := '';
if XNumero <> Null then
begin
cNumero := IntToStr(XNumero);
LDec := True;
for nInd := 1 to Length(CNumero) do
begin
nDigito := StrToInt(cNumero[nInd]);
nPos := Length(CNumero) + 1 - nInd;
if nDigito <> 0 then
begin
if nPos in [3,6,9,12] then
if nDigito = 1 then
if (StrToInt(cNumero[nInd+1]) = 0) and (StrToInt(cNumero[nInd+2]) = 0) then
Result := Result + Centenas[nDigito]
else
Result := Result + 'Ciento '
else
Result := Result + Centenas[nDigito]
else
if nPos in [2,5,8,11] then
begin
Ldec := True;
if nDigito = 1 then
begin
Result := Result + Decimos[StrToInt(cNumero[nInd+1])];
LDec := False;
end
else
if nDigito = 2 then
if (StrToInt(cNumero[nInd+1]) = 0) then
Result := Result + Decenas[nDigito]
else
Result := Result + 'Veinte y '
else
Result := Result + Decenas[nDigito];
if not(nDigito in [0,1,2]) and (StrToInt(cNumero[nInd+1]) <> 0) then
Result := Result + 'y ';
end
else
if (nPos in [1,4,7,10]) and LDec then
if (nDigito <> 1) then
Result := Result + Unidades[nDigito]
else
if nPos in [1,7] then
Result := Result + Unidades[nDigito];
end;
if nPos = 4 then
if not((StrToInt(cNumero[nInd]) = 0) and (StrToInt(cNumero[nInd-1]) = 0) and (StrToInt(cNumero[nInd-2]) = 0)) then
Result := Result + 'Mil ';
if nPos = 7 then
if (nDigito = 1) and (Length(CNumero) = 7) then
Result := Result + 'Millon '
else
Result := Result + 'Millones ';
if nPos = 10 then
Result := Result + 'Mil ';
end;
end;
end;