Number to words delphi function

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


  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';   


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

    //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
        bAllZeros := 0;
        if i = 1 Then

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

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

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

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

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

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

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

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

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

    0 :  //Hundreds position


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


  Result := strResult;

  Result := '';


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.


Anonymous said...

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

golapi khatun said...

Wellcome to | 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, 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, 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"?

bakulkhankhetlal said...

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

Post a Comment