Results 1 to 2 of 2

Thread: [DELPHI] Problem using function

  1. #1

    Thread Starter
    Fanatic Member esposito's Avatar
    Join Date
    Sep 2003
    Location
    Perugia, Italy
    Posts
    961

    [DELPHI] Problem using function

    Hello, I am new to Delphi. I only started to take a look at this language a few days ago, while looking around to find an alternative to VB6.

    I have downloaded the code of a function from an Italian Web site that is supposed to convert Currency numbers into strings. The code runs as follows:

    Code:
    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 ;
    I have tried to call the function in the following way, but without success:

    Code:
    procedure TForm1.cmdConvertiClick(Sender: TObject);
    begin
    
    	lblStringa.Caption := NumeroToStringa(StrToCurr(txtNumero.Text));
    
    end;
    where lblStringa is a Label and txtNumero an Edit box.

    What I get is just a few '#' characters and the conversion does not take place.

    Can anybody tell me where the mistake is?

    TIA

  2. #2

    Thread Starter
    Fanatic Member esposito's Avatar
    Join Date
    Sep 2003
    Location
    Perugia, Italy
    Posts
    961

    Re: [DELPHI] Problem using function

    Problem solved. Thank you anyway.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width