Visit our Sponsor   Visit our Sponsor
delphi3000.com - the free delphi knowledge platform
delphi3000.com - the free delphi knowledge platform
Have a look at your member-status

connecting people's knowledge


  - Recent ArticlesRSS feed for Recent Articles on delphi3000.com
  - List of All Articles
  - Top Viewed Articles
  - Articles (+Attachem.)
  - Articles Of Interest
  - Categories
  - Top Uploader
  - Search
  - Index

  - My Home
  - Submit an Article
  - My Articles
  - My Personal Data
  - My Bookmarks
  - Activities
  - Login/Logout

  - Sign Up
  - Why Sign Up
  - Newsletter

  - Press
  - Advertise

  - Contact
  - Feedback





Community
Borland
ClubeDelphi
Dr. Bob
UK-BUG
Delphi Meetings
Planeta Delphi







Startblatt.de






Share this article with friendsShare this article with friends
Rate this articleRate this article - to keep the quality of delphi3000.com !
Comment this article or read through previous comments (0)


Convert numbers to words.Go to Abdulaziz Jasser's websiteFormat this article printer-friendly!Bookmark function is only available for registered users!
Use it for all your needs (Checks, Invoices…) and best of all: It is FREE.
Product:
Delphi all versions
Category:
Others
Skill Level:
Scoring:
Last Update:
03/02/2003
Search Keys:
delphi delphi3000 article borland vcl code-snippet convert numbers words currency
Times Scored:
5
Visits:
4064
Uploader: Abdulaziz Jasser
Company: Saudi Telecommunications Compa
Reference: N/A
 
Question/Problem/Abstract:
Do you want to know how to convert numbers to words?
Answer:



Function GetNumberName(cValue : Currency; sCurrencyName1 : String = ''; sCurrencyName2 : String = '') : String;
var
         i      : Integer;
         iInt   : Int64;
         iDec   : Integer;
         sNo    : String;
         sSpell : array[1..90] of String;
begin
         if cValue = 0 then begin
            Result := 'Zero';
            exit;
         end;

         //If currency name is not passed , then Saudi Riyals are used.
         if sCurrencyName1 = '' then
            sCurrencyName1 := ' Riyal(s)';

         if sCurrencyName2 = '' then
            sCurrencyName2 := ' Halla(s)';

         sSpell[1]  := 'One';
         sSpell[2]  := 'Two';
         sSpell[3]  := 'Three';
         sSpell[4]  := 'Four';
         sSpell[5]  := 'Five';
         sSpell[6]  := 'Six';
         sSpell[7]  := 'Seven';
         sSpell[8]  := 'Eight';
         sSpell[9]  := 'Nine';
         sSpell[10] := 'Ten';
         sSpell[11] := 'Eleven';
         sSpell[12] := 'Twelve';
         sSpell[13] := 'Thirteen';
         sSpell[14] := 'Fourteen';
         sSpell[15] := 'Fifteen';
         sSpell[16] := 'Sixteen';
         sSpell[17] := 'Seventeen';
         sSpell[18] := 'Eighteen';
         sSpell[19] := 'Nineteen';
         sSpell[20] := 'Twenty';
         sSpell[30] := 'Thirty';
         sSpell[40] := 'Forty';
         sSpell[50] := 'Fifty';
         sSpell[60] := 'Sixty';
         sSpell[70] := 'Seventy';
         sSpell[80] := 'Eighty';
         sSpell[90] := 'Ninety';

         sNo := Trim(CurrToStrF(cValue,ffCurrency,CurrencyDecimals));

         i := Pos('.', sNo);
         if i = 0 then
            iDec := 0
         else
            iDec := StrToInt(Copy(sNo,i+1, 2));

         i := Pos('.', sNo);
         if i = 0 then
            iInt := StrToInt(sNo)
         else
            iInt := StrToInt(Copy(sNo,0, i-1));

while iInt > 0 do begin
               if (Length(Result) > 0) and (iInt > 9)  then
                  Result := Result + ' and ';

       if iInt <= 20 then begin
                  Result := Result + sSpell[iInt];
                  Dec(iInt, iInt);
               end;

       if (iInt > 9) and (iInt <= 99) then begin
                  if iInt mod 10 = 0 then begin
                     Result := Result + sSpell[iInt];
                     Dec(iInt, iInt);
                  end else begin
                     i := StrToInt(Copy(IntToStr(iInt),0,1)) * 10;
                     Result := Result + sSpell[i] + '-';
                     Dec(iInt, i);
                  end;
               end;

       if (iInt > 99) and (iInt <= 999) then begin
                  i := StrToInt(Copy(IntToStr(iInt),0,1));
                  Result := Result + sSpell[i] + ' Hundred(s)';
                  Dec(iInt, i*100);
               end;

       if (iInt > 999) and (iInt <= 9999) then begin
                  i := StrToInt(Copy(IntToStr(iInt),0,1));
                  Result := Result + sSpell[i] + 'Thousand(s)';

                  Dec(iInt, i*1000);
               end;

               if (iInt > 9999) and (iInt <= 99999) then begin
                  i := StrToInt(Copy(IntToStr(iInt), 0, 2));
                  Result := Result + sSpell[i] + ' Thousand(s)';
                  Dec(iInt, i*1000);
               end;

               if (iInt > 99999) and (iInt <= 999999) then begin
                  i := StrToInt(Copy(IntToStr(iInt), 0, 1));
                  Result := Result + sSpell[i] + ' Hundred(s)';
                  Dec(iInt, i*100000);
               end;

               if (iInt > 999999) and (iInt <= 9999999) then begin
                  i := StrToInt(Copy(IntToStr(iInt), 0, 1));
                  Result := Result + sSpell[i] + ' Million(s) ';

                  Dec(iInt, i*1000000);
               end;

               if (iInt > 9999999) and (iInt <= 99999999) then begin
                  i := StrToInt(Copy(IntToStr(iInt), 0, 1))*10;
                  Result := Result + sSpell[i] + '-';
                  Dec(iInt, i*1000000);

                  i := StrToInt(Copy(IntToStr(iInt), 0, 1));
                  Result := Result + sSpell[i] + ' Million(s)';
                  Dec(iInt, i*1000000);
               end;

               if (iInt > 99999999) and (iInt <= 999999999) then begin
                  i := StrToInt(Copy(IntToStr(iInt), 0, 1));
                  Result := Result + sSpell[i] + ' Hundred(s) ';
                  Dec(iInt, i*100000000);

                  i := StrToInt(Copy(IntToStr(iInt), 0, 1))*10;
                  Result := Result + sSpell[i] + '-';
                  Dec(iInt, i*1000000);

                  i := StrToInt(Copy(IntToStr(iInt), 0, 1));
                  Result := Result + sSpell[i] + ' Million(s)';
                  Dec(iInt, i*1000000);
               end;
          end;

          Result := Result + sCurrencyName1;

          if iDec > 0 then
             Result := Result + ' and ';

          while iDec > 0 do begin
                if (iDec > 0) and (iDec < 10) then begin
                   Result := Result + sSpell[iDec] + sCurrencyName2;
                   Dec(iDec, iDec);
                end else begin
                   i := StrToInt(Copy(IntToStr(iDec), 0, 1))*10;
                   Result := Result + sSpell[i] + '-';
                   Dec(iDec, i);
                end;
          end;
end;



Example of use:
Label1.Caption := GetNumberName(1234);
Label1.Caption := GetNumberName(1234,'Dollars','Cents');





Please rate this article!
Skill level:
BeginnerExpert

Useful:
No!Very!

Overall rating:
PoorExcellent



Comments to this article
Write a new comment













 
Sign up to consume product discounts for Bronze memberships !

read more


  Visit our Sponsor

 

  Community Ad of
L. Rosenstein
 
   














 







     
  Copyright © 2000 - 2007 delphi3000.com - All rights reserved. Terms of use. || Privacy
delphi3000.com is a service by bluestep.com IT-Services GmbH (Vienna)