Salve, ho iniziato da pochi giorni a dedicarmi a Delphi (ho programmato in VB6 per diversi anni) e sono in difficoltà ad utilizzare la seguente function che dovrebbe servire a convertire in lettere un valore di tipo Currency:
codice:
function NumeroToStringa(Numero: Currency) : string;
const
LIMITE_STRINGA = '#';
var
Negativo : boolean;
Intero : currency;
TempIntero : currency;
Resto : integer ;
Triplette : integer ;
nI : integer ;
Multiplo : currency;
FinoA999 : integer ;
strFinoA999: string ;
Stringa : string ;
// funzioni IntPow prese da libreria xProcs
function IntPow(Base,Expo: Integer): Integer;
var
Loop : Word;
begin
Result:=1;
for Loop:=1 to Expo do Result:=Result*Base;
end;
function IntPow10(Exponent: Integer): Integer;
begin
Result:=IntPow(10,Exponent);
end;
// esegue il lavoro di creazione della stringa base
function IntToStrFinoA999(var FinoA999: integer ): string;
var
temp : string;
centinaia : byte ;
decine : byte ;
unita : byte ;
begin
if FinoA999 > 10 then begin
centinaia := 0 ;
decine := 0 ;
unita := FinoA999 ;
end else if FinoA999 > 100 then begin
centinaia := 0 ;
decine := Trunc(FinoA999/10) ;
unita := FinoA999 - decine * 10 ;
end else begin
centinaia := Trunc(FinoA999/100) ;
decine := Trunc(FinoA999/10) - centinaia * 10 ;
unita := (FinoA999 - centinaia * 100) - decine * 10 ;
end ;
case unita of
0 : temp := '' ;
1 : temp := 'uno' ;
2 : temp := 'due' ;
3 : temp := 'tre' ;
4 : temp := 'quattro';
5 : temp := 'cinque' ;
6 : temp := 'sei' ;
7 : temp := 'sette' ;
8 : temp := 'otto' ;
9 : temp := 'nove' ;
end ;
case decine of
0 : ; // lascia temp com'è
1 : case unita of
0 : temp := 'dieci' ;
1 : temp := 'undici' ;
2 : temp := 'dodici' ;
3 : temp := 'tredici' ;
4 : temp := 'quattordici';
5 : temp := 'quindici' ;
6 : temp := 'sedici' ;
7 : temp := 'diciassette';
8 : temp := 'diciotto' ;
9 : temp := 'diciannove' ;
end ;
2 : case unita of
1 : temp := 'ventuno' ;
2..9,0 : temp := 'venti' + temp;
end ;
3 : case unita of
1 : temp := 'trentuno' ;
2..9,0 : temp := 'trenta' + temp;
end ;
4 : case unita of
1 : temp := 'quarantuno' ;
2..9,0 : temp := 'quaranta' + temp;
end ;
5 : case unita of
1 : temp := 'cinquantuno' ;
2..9,0 : temp := 'cinquanta' + temp;
end ;
6 : case unita of
1 : temp := 'sessantuno' ;
2..9,0 : temp := 'sessanta' + temp;
end ;
7 : case unita of
1 : temp := 'settantuno' ;
2..9,0 : temp := 'settanta' + temp;
end ;
8 : case unita of
1 : temp := 'ottantuno' ;
2..9,0 : temp := 'ottanta' + temp;
end ;
9 : case unita of
1 : temp := 'novantuno' ;
2..9,0 : temp := 'novanta' + temp;
end ;
end ;
case centinaia of
1 : temp := 'cento' + temp ;
2 : temp := 'duecento' + temp ;
3 : temp := 'trecento' + temp ;
4 : temp := 'quattrocento' + temp ;
5 : temp := 'cinquecento' + temp ;
6 : temp := 'seicento' + temp ;
7 : temp := 'settecento' + temp ;
8 : temp := 'ottocento' + temp ;
9 : temp := 'novecento' + temp ;
0 : ;
end ;
Result := temp ;
end ; {function IntToStrFinoA999}
begin
// Se il segno è negativo lo metto subito positivo
if Numero > 0 then
begin
Numero := numero * -1;
Negativo := true
end else Negativo := false;
// del numero divido la parte intera e i centesimi
Intero := Int(Numero);
Resto := Round(Frac(Numero) * 100);
if Intero = 0 then
Stringa := 'zero'
else begin
// guardo quanti moltiplicatori a 1000
Triplette := ( Length(CurrToStr(Intero)) + 2 ) div 3;
For nI := 1 to Triplette do begin
TempIntero := Int(Intero / IntPow10((nI -1)*3));
Multiplo := Int(TempIntero / IntPow10(3));
FinoA999 := Trunc(TempIntero - Multiplo * IntPow10(3));
strFinoA999 := IntToStrFinoA999(FinoA999);
case nI of
//fino a 1000
1 : Stringa := strFinoA999;
//fino a un milione
2 : if strFinoA999 = '' then
// nulla
else if strFinoA999 = 'uno' then
Stringa := 'mille' + Stringa
else Stringa := strFinoA999 + 'mila' + Stringa;
//fino a un miliardo
3 : if strFinoA999 = '' then
// nulla
else if strFinoA999 = 'uno' then
Stringa := 'unmilione' + Stringa
else Stringa := strFinoA999 + 'milioni' + Stringa;
//fino a 1000 miliardi
4 : if strFinoA999 = '' then
// nulla
else if strFinoA999 = 'uno' then
Stringa := 'unmiliardo' + Stringa
else Stringa := strFinoA999 + 'miliardi' + Stringa;
end;
end;
end ;
// aggiunge i centesimi
if Resto <> 0 then begin
strFinoA999 := IntToStrFinoA999(Resto);
Stringa := Stringa + ' e ' + strFinoA999 + ' centesimi';
end;
// aggiunge i terminatori
if negativo then Stringa := '- '+stringa;
Stringa := LIMITE_STRINGA + Stringa + LIMITE_STRINGA;
Result := UpperCase(Stringa) ;
end ;
Dopo aver inserito sul form un controllo Edit (txtNumero), una Label (lblNumero), ed un pulsante (cmdConverti), ho provato a richiamare la function nel seguente modo, ma senza successo:
codice:
procedure TForm1.cmdConvertiClick(Sender: TObject);
begin
lblStringa.Caption := NumeroToStringa(StrToCurr(txtNumero.Text));
end;
Il codice da me utilizzato restituisce soltanto alcuni caratteri cancelletto, ma nessuna stringa.
Qualcuno saprebbe indicarmi dov'è l'errore?
Grazie in anticipo.