Ciao a tutti,
ho scritto questa funzione per convertire una certa cifra (somma di denaro nella fattispecie) in lettere:
codice:
function CifraLettere(cifra: string): string; export;
function NrUnita(cifra: char): string;
begin
case cifra of
'1': Result := 'uno'; '2': Result := 'due';
'3': Result := 'tre'; '4': Result := 'quattro';
'5': Result := 'cinque'; '6': Result := 'sei';
'7': Result := 'sette'; '8': Result := 'otto';
'9': Result := 'nove';
end;
end;
var lettere: string;
begin
//Trasformazione MIGLIAIA
if Length(cifra) = 4 then
if cifra[1] = '1' then lettere := 'mille'
else lettere := NrUnita(cifra[1]) + 'mila';
//Trasformazione CENTINAIA
if (Length(cifra) = 4) or (Length(cifra) = 3) then
if not (cifra[Length(cifra) - 2] = '0') then
if cifra[Length(cifra) - 2] = '1' then lettere := lettere + 'cento'
else lettere := lettere + NrUnita(cifra[Length(cifra) - 2]) + 'cento';
//Trasformazione DECINE
if (Length(cifra) >= 2) and not (StrToInt(AnsiLeftStr(AnsiRightStr(cifra, 2), 1)) = 1)
and not (StrToInt(AnsiRightStr(cifra, 1)) = 1) and not (StrToInt(AnsiRightStr(cifra, 1)) = 8) then
begin
case cifra[Length(cifra) - 1] of
'2': lettere := lettere +'venti'; '3': lettere := lettere +'trenta';
'4': lettere := lettere +'quaranta'; '5': lettere := lettere +'cinquanta';
'6': lettere := lettere +'sessanta'; '7': lettere := lettere +'settanta';
'8': lettere := lettere +'ottanta'; '9': lettere := lettere +'novanta';
end;
end else
if (Length(cifra) >= 2) and ((StrToInt(AnsiRightStr(cifra, 1)) = 1) and not (StrToInt(AnsiLeftStr(AnsiRightStr(cifra, 2), 1)) = 1)) or (StrToInt(AnsiRightStr(cifra, 1)) = 8) then
begin
case cifra[Length(cifra) - 1] of
'2': lettere := lettere +'vent'; '3': lettere := lettere +'trent';
'4': lettere := lettere +'quarant'; '5': lettere := lettere +'cinquant';
'6': lettere := lettere +'sessant'; '7': lettere := lettere +'settant';
'8': lettere := lettere +'ottant'; '9': lettere := lettere +'novant';
end;
end else
if (Length(cifra) >= 2) and (StrToInt(AnsiLeftStr(AnsiRightStr(cifra, 2), 1)) = 1) then
begin
case StrToInt(AnsiRightStr(cifra, 2)) of
10: lettere := lettere +'dieci'; 11: lettere := lettere +'undici';
12: lettere := lettere +'dodici'; 13: lettere := lettere +'tredici';
14: lettere := lettere +'quattordici'; 15: lettere := lettere +'quindici';
16: lettere := lettere +'sedici'; 17: lettere := lettere +'diciassette';
18: lettere := lettere +'diciotto'; 19: lettere := lettere +'diciannove';
end;
end;
//Trasformazione UNITA'
if (Length(Cifra) >= 1) then
if (Length(cifra) >= 2) and ((StrToInt(AnsiLeftStr(AnsiRightStr(cifra, 2), 1)) > 1) or (StrToInt(AnsiLeftStr(AnsiRightStr(cifra, 2), 1)) = 0)) then
lettere := lettere + NrUnita(cifra[Length(cifra)])
else if (Length(cifra) = 1) then
lettere := lettere + NrUnita(cifra[Length(cifra)]);
Result := lettere;
end;
La funzione non gestisce i decimali in quanto essi vengono eventualmente aggiunti come "/xx" dopo la cifra in lettere:
es. 1999,31 --> millenovecentonovantanove / 31
La posto prima di tutto per condividerla... può sempre essere utile. In secondo luogo vi chiedo: è possibile ottimizzarla o migliorarla?