Sponsors

Number to words delphi function

http://rahsoft.blogspot.com
After so many for a long time looked for the function delphi the number to the sentence, finally had met also. For you that needed, take note the function below this.


Function NumToWords(dblValue : Double) : String;
var
  ones : array[0..9] of String;
  teens : array[0..9] of String;
  tens : array[0..9] of String;
  thousands : array[0..4] of String;
  i, nPosition, nDigit, bAllZeros : Integer;
  strResult, strTemp, tmpBuff : String;

begin

  ones[0] := 'ZERO';
  ones[1] := 'ONE';
  ones[2] := 'TWO';
  ones[3] := 'THREE';
  ones[4] := 'FOUR';
  ones[5] := 'FIVE';
  ones[6] := 'SIX';
  ones[7] := 'SEVEN';
  ones[8] := 'EIGHT';
  ones[9] := 'NINE';

  teens[0] := 'TEN';
  teens[1] := 'ELEVEN';
  teens[2] := 'TWELVE';
  teens[3] := 'THIRTEEN';
  teens[4] := 'FOURTEEN';
  teens[5] := 'FIFTEEN';
  teens[6] := 'SIXTEEN';
  teens[7] := 'SEVENTEEN';
  teens[8] := 'EIGHTEEN';
  teens[9] := 'NINETEEN';

  tens[0] := '';
  tens[1] := ''; //TEN1

  tens[2] := 'TWENTY';
  tens[3] := 'THIRTY';
  tens[4] := 'FORTY';
  tens[5] := 'FIFTY';
  tens[6] := 'SIXTY';
  tens[7] := 'SEVENTY';
  tens[8] := 'EIGHTY';
  tens[9] := 'NINETY';

  thousands[0] := '';
  thousands[1] := 'THOUSAND';
  thousands[2] := 'MILLION';
  thousands[3] := 'BILLION';
  thousands[4] := 'TRILLION';   

 try

  strResult := '';
  strTemp := IntToStr(round(dblValue));
  //Iterate through string
  For i := Length(strTemp) downTo 1 do
  begin

    //Get value of this digit
    nDigit := StrToInt(MidStr(strTemp, i, 1));
    //Get column position
    nPosition := (Length(strTemp) - i) + 1;
    //Action depends on 1's, 10's or 100's column
    //Select Case (nPosition Mod 3)
    case (nPosition Mod 3) of

    1 :  //Case 1  //'1's position
    begin
        bAllZeros := 0;
        if i = 1 Then

          tmpBuff := ones[nDigit] + ' '
        else
        if MidStr(strTemp, i - 1, 1) = '1' Then

        begin
          tmpBuff := teens[nDigit] + ' ';
        end
        else
        if nDigit > 0 Then

          tmpBuff := ones[nDigit] + ' '
        else
        begin
          //If next 10s & 100s columns are also
          //zero, then don't show 'thousands'
          bAllZeros := 1;
          if i > 1 Then

          begin
            If MidStr(strTemp, i - 1, 1) <> '0' Then
              bAllZeros := 0;
          end;
          If i > 2 Then

          begin
            If MidStr(strTemp, i - 2, 1) <> '0' Then
              bAllZeros := 0;

          End;
          tmpBuff := '';
        end;
        If (bAllZeros = 0) and (nPosition > 1) Then

          tmpBuff := tmpBuff + thousands[nPosition div 3] + ' ';

        strResult := tmpBuff + strResult;
        
    end;
    2 :  //Tens position
    begin

        if nDigit > 0 Then
          //if MidStr(strTemp, i - 1, 1) <> '1' Then
          strResult := tens[nDigit] +  ' ' + strResult;

    end;
    0 :  //Hundreds position

    begin

        if nDigit > 0 Then
          strResult := ones[nDigit] + ' HUNDRED ' + strResult;

    end;
    end;
  end;

  Result := strResult;
  
 except

  Result := '';
 end;

end;

examples of such usage :

Label1.Caption := NumToWords('your variable');

Just simple right ? 

you can also try this Delphi function

in the next post I will give a delphi function, number to words the Indonesian version.


Source : forget the source of this.


                                                

Post a Comment

4 Comments

  1. Not working. Hard to understand.Make it simple.Im a beginner in delphi

    ReplyDelete
  2. Excellent.... thanks, just a question, What is MidStr?

    ReplyDelete
  3. You can found MidStr on StrUtils unit (you need declare it).

    I have other question, How can I add "Cents"?

    ReplyDelete
  4. Nice to see the attractive post. I am very happy to see this post. It helped everybody at all.
    The personal statement is a document that people often struggle mightily with, and this is coupled with a good deal of pressure considering how important it is to your success in getting a neurosurgery online residency.
    neurosurgery residency

    ReplyDelete