Number to words delphi function

Posted by Rahman M On Sunday, July 15, 2012 4 comments
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.


4 comments:

Anonymous said...

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

golapi khatun said...

Wellcome to Exkash.com | Cashout E-curreny to Bank Account Directly.
There is no doubt about the fact that the Internet is the best place to sell your perfect money for local cash, the reason being that there are thousands of prospective buyers who are willing to pay any amount to get your Perfect money funds therefore you can capitalize on this to make a significant gain by selling to them. Perfect money is quite scarce for many interested buyers and buying from third parties can cost some fortune, however you can sell at discounted rates to prospective buyers directly from your Perfect money account.
The best place to sell your Perfect money for local cash is exkash.com, this website has a huge market place where interested buyers can easily locate you and then you can set your own prices and negotiate with the buyers online. Once you have completed pricing negotiation with interested buyers, Exkash.com will then help you collect the funds in your Perfect money account where you can proceed to request for bank transfer.
okpay to bank
Cashout perfect money to bank account
Perfectmoney Debit card
Egopay to bank
Bitcoin to Bank account
Bitcoin Debit card

Anonymous said...

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

Anonymous said...

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

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

Post a Comment