<

Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x

[Delphi Code] Number to Word

Published on
11,790 Points
5,490 Views
3 Endorsements
Last Modified:
Objective:
- This article will help user in how to convert their numeric value become words.

How to use
1. You can copy this code in your Unit as function
2. than you can perform your function by type this code

The Code
 
function TISupport.NTW(ChkER: boolean; ER: string; D: double): string;
const
  Ones: array[0..9] of string = ('Zero', 'One', 'Two', 'Three', 'Four', 'Five', 'Six', 'Seven', 'Eight', 'Nine');
  Teens: array[10..19] of string = ('Ten', 'Eleven', 'Twelve', 'Thirteen', 'Fourteen', 'Fifteen', 'Sixteen', 'Seventeen', 'Eighteen', 'Nineteen');
  Tens: array[2..9] of string = ('Twenty', 'Thirty', 'Forty', 'Fifty', 'Sixty', 'Seventy', 'Eighty', 'Ninety');
  Suffix: array[0..5] of string = ('Hundred', 'Thousand', 'Million', 'Billion', 'Trillion', 'Quadrillion');
var RStr, sDec, sFrac: string;
    vFrac: double;
    I, vDec : integer;
    TruncTens, TruncHund, TruncThou, TruncMio, TruncBio, TruncTril, iD: Int64;
    ReadFrac: boolean; 
    function fTensENG(xD: integer): string;
    var BTStr: string;
    begin
       if (xD >= 0) and (xD <= 9) then BTStr := Ones[xD] else
       if (xD >= 10) and (xD <= 19) then BTStr := Teens[xD] else
       if (xD >= 20) and (xD <= 99) then
       begin
          if (StrToInt(RightStr(IntToStr(xD), 1)) = 0) then
             BTStr := Tens[StrToInt(LeftStr(IntToStr(xD), 1))]
          else
             BTStr := Tens[StrToInt(LeftStr(IntToStr(xD), 1))] + ' ' +
                      Ones[StrToInt(RightStr(IntToStr(xD), 1))]
       end;
       Result := BTStr;
    end; 
    function fHundENG(xD: integer): string;
    var BTStr: string;
    begin
       BTStr := Ones[StrToInt(LeftStr(IntToStr(xD), 1))] + ' ' + Suffix[0];
       TruncTens := StrToInt(RightStr(IntToStr(xD), 2));
       if (TruncTens <> 0) then
          BTStr := BTStr + ' and ' + fTensENG(TruncTens);
       Result := BTStr;
    end; 
    function fThouENG(xD: Integer): string;
    var BTStr: string;
    begin
       if (xD >= 1000) and (xD <= 9999) then
       begin
          BTStr := Ones[StrToInt(LeftStr(IntToStr(xD), 1))] + ' ' + Suffix[1];
          TruncHund := StrToInt(RightStr(IntToStr(xD), 3));
          TruncTens := StrToInt(RightStr(IntToStr(xD), 2));
          if (TruncHund <> 0) and (TruncTens = 0) then
             BTStr := BTStr + 'and ' + Ones[StrToInt(LeftStr(IntToStr(xD), 1))] + ' ' + Suffix[0]
          else
          if (TruncHund <> 0) and (TruncTens <> 0) then
             BTStr := BTStr + ', ' + fHundENG(TruncHund);
       end
       else
       if (Trunc(xD) >= 10000) and (Trunc(xD) <= 19999) then
       begin
          BTStr := Teens[StrToInt(LeftStr(IntToStr(xD), 2))] + ' ' + Suffix[1];
          TruncHund := StrToInt(RightStr(IntToStr(xD), 3));
          TruncTens := StrToInt(RightStr(IntToStr(xD), 2));
          if (TruncHund <> 0) and (TruncTens = 0) then
             BTStr := BTStr + ' and ' + Ones[StrToInt(LeftStr(IntToStr(TruncHund), 1))] + ' ' + Suffix[0]
          else
          if (TruncHund <> 0) and (TruncTens <> 0) then
             BTStr := BTStr + ', ' + fHundENG(TruncHund);
       end
       else
       if (Trunc(xD) >= 20000) and (Trunc(xD) <= 99999) then
       begin
          if (StrToInt(MidStr(IntToStr(xD), 2, 1)) = 0) then
             BTStr := Tens[StrToInt(LeftStr(IntToStr(xD), 1))] + ' ' + Suffix[1]
          else
             BTStr := Tens[StrToInt(LeftStr(IntToStr(xD), 1))] + ' ' +
                      Ones[StrToInt(MidStr(IntToStr(xD), 2, 1))] + ' ' + Suffix[1];
          TruncHund := StrToInt(RightStr(IntToStr(xD), 3));
          TruncTens := StrToInt(RightStr(IntToStr(xD), 2));
          if (TruncHund <> 0) and (TruncTens = 0) then
             BTStr := BTStr + 'and ' + Ones[StrToInt(LeftStr(IntToStr(TruncHund), 1))] + ' ' + Suffix[0]
          else
          if (TruncHund <> 0) and (TruncTens <> 0) then
             BTStr := BTStr + ', ' + fHundENG(TruncHund);
       end
       else
       if (xD >= 100000) and (xD <= 9999999) then
       begin
          BTStr := fHundENG(StrToInt(LeftStr(IntToStr(xD), 3))) + ' ' + Suffix[1];
          TruncHund := StrToInt(RightStr(IntToStr(xD), 3));
          TruncTens := StrToInt(RightStr(IntToStr(xD), 2));
          if (TruncHund <> 0) and (TruncTens = 0) then
             BTStr := BTStr + 'and ' + Ones[StrToInt(LeftStr(IntToStr(xD), 1))] + ' ' + Suffix[0]
          else
          if (TruncHund <> 0) and (TruncTens <> 0) then
             BTStr := BTStr + ', ' + fHundENG(TruncHund);
       end;
       Result := BTStr;
    end; 
    function fMioENG(xD: Int64): string;
    var BTStr: string;
    begin
       if (xD >= 1000000) and (xD <= 9999999) then
       begin
          BTStr := Ones[StrToInt(LeftStr(IntToStr(xD), 1))] + ' ' + Suffix[2];
          TruncThou := StrToInt(RightStr(IntToStr(xD), 6));
          if (TruncThou <> 0) then
             BTStr := BTStr + ', ' + fThouENG(TruncThou);
       end
       else
       if (xD >= 10000000) and (xD <= 19999999) then
       begin
          BTStr := Teens[StrToInt(LeftStr(IntToStr(xD), 2))] + ' ' + Suffix[2];
          TruncThou := StrToInt(RightStr(IntToStr(xD), 6));
          if (TruncThou <> 0) then
             BTStr := BTStr + ', ' + fThouENG(TruncThou);
       end
       else
       if (xD >= 20000000) and (xD <= 99999999) then
       begin
          if (StrToInt(LeftStr(IntToStr(xD), 2)) = 0) then
             BTStr := Tens[StrToInt(LeftStr(IntToStr(xD), 2))] + ' ' + Suffix[2]
          else
             BTStr := Tens[StrToInt(LeftStr(IntToStr(xD), 2))] + ' ' +
                      Ones[StrToInt(LeftStr(IntToStr(xD), 2))] + ' ' + Suffix[2];
          TruncThou := StrToInt(RightStr(IntToStr(xD), 6));
          if (TruncThou <> 0) then
             BTStr := BTStr + ', ' + fThouENG(TruncThou);
       end
       else
       if (xD >= 100000000) and (xD <= 999999999) then
       begin
          BTStr := fHundENG(StrToInt(LeftStr(IntToStr(xD), 3))) + ' ' + Suffix[2];
          TruncThou := StrToInt(RightStr(IntToStr(xD), 6));
          if (TruncThou <> 0) then
             BTStr := BTStr + ', ' + fThouENG(TruncThou);
       end
       else
       begin
          TruncThou := StrToInt(RightStr(IntToStr(xD), 6));
          if (TruncThou <> 0) then
             BTStr := BTStr + fThouENG(TruncThou);
       end;
       Result := BTStr;
    end; 
    function fBioENG(xD: Int64): string;
    var BTStr: string;
    begin
       if (xD >= 1000000000) and (xD <= 9999999999) then
       begin
          BTStr := Ones[StrToInt64(LeftStr(IntToStr(xD), 1))] + ' ' + Suffix[3];
          TruncMio := StrToInt64(RightStr(IntToStr(xD), 9));
          if (TruncMio <> 0) then
             BTStr := BTStr + ', ' + fMioENG(TruncMio);
       end
       else
       if (xD >= 10000000000) and (xD <= 19999999999) then
       begin
          BTStr := Teens[StrToInt64(LeftStr(IntToStr(xD), 2))] + ' ' + Suffix[3];
          TruncMio := StrToInt64(RightStr(IntToStr(xD), 9));
          if (TruncMio <> 0) then
             BTStr := BTStr + ', ' + fMioENG(TruncMio);
       end
       else
       if (xD >= 20000000000) and (xD <= 99999999999) then
       begin
          if (StrToInt64(LeftStr(IntToStr(xD), 2)) = 0) then
             BTStr := Tens[StrToInt64(LeftStr(IntToStr(xD), 2))] + ' ' + Suffix[3]
          else
             BTStr := Tens[StrToInt64(LeftStr(IntToStr(xD), 2))] + ' ' +
                      Ones[StrToInt64(LeftStr(IntToStr(xD), 2))] + ' ' + Suffix[3];
          TruncMio := StrToInt64(RightStr(IntToStr(xD), 9));
          if (TruncMio <> 0) then
             BTStr := BTStr + ', ' + fMioENG(TruncMio);
       end
       else
       if (xD >= 100000000000) and (xD <= 999999999999) then
       begin
          BTStr := fHundENG(StrToInt64(LeftStr(IntToStr(xD), 3))) + ' ' + Suffix[3];
          TruncMio := StrToInt64(RightStr(IntToStr(xD), 9));
          if (TruncMio <> 0) then
             BTStr := BTStr + ', ' + fMioENG(TruncMio);
       end
       else
       begin
          TruncMio := StrToInt64(RightStr(IntToStr(xD), 9));
          if (TruncMio <> 0) then
             BTStr := BTStr + fMioENG(TruncMio);
       end;
       Result := BTStr;
    end; 
    function fTrilENG(xD: Int64): string;
    var BTStr: string;
    begin
       if (xD >= 1000000000000) and (xD <= 9999999999999) then
       begin
          BTStr := Ones[StrToInt(LeftStr(IntToStr(xD), 1))] + ' ' + Suffix[4];
          TruncBio := StrToInt64(RightStr(IntToStr(xD), 12));
          if (TruncBio <> 0) then
             BTStr := BTStr + ', ' + fBioENG(TruncBio);
       end
       else
       if (xD >= 10000000000000) and (xD <= 19999999999999) then
       begin
          BTStr := Teens[StrToInt(LeftStr(IntToStr(xD), 2))] + ' ' + Suffix[4];
          TruncBio := StrToInt64(RightStr(IntToStr(xD), 12));
          if (TruncBio <> 0) then
             BTStr := BTStr + ', ' + fBioENG(TruncBio);
       end
       else
       if (xD >= 20000000000000) and (xD <= 99999999999999) then
       begin
          if (StrToInt64(LeftStr(IntToStr(xD), 2)) = 0) then
             BTStr := Tens[StrToInt64(LeftStr(IntToStr(xD), 2))] + ' ' + Suffix[4]
          else
             BTStr := Tens[StrToInt64(LeftStr(IntToStr(xD), 2))] + ' ' +
                      Ones[StrToInt64(LeftStr(IntToStr(xD), 2))] + ' ' + Suffix[4];
          TruncBio := StrToInt64(RightStr(IntToStr(xD), 12));
          if (TruncBio <> 0) then
             BTStr := BTStr + ', ' + fBioENG(TruncBio);
       end
       else
       if (xD >= 100000000000000) and (xD <= 999999999999999) then
       begin
          BTStr := fHundENG(StrToInt64(LeftStr(IntToStr(xD), 3))) + ' ' + Suffix[4];
          TruncBio := StrToInt64(RightStr(IntToStr(xD), 12));
          if (TruncBio <> 0) then
             BTStr := BTStr + ', ' + fBioENG(TruncBio);
       end
       else
       begin
          TruncBio := StrToInt64(RightStr(IntToStr(xD), 12));
          if (TruncBio <> 0) then
             BTStr := BTStr + fBioENG(TruncBio);
       end;
       Result := BTStr;
    end; 
    function fQuadENG(xD: Int64): string;
    var BTStr: string;
    begin
       if (xD >= 1000000000000000) and (xD <= 9999999999999999) then
       begin
          BTStr := Ones[StrToInt(LeftStr(IntToStr(xD), 1))] + ' ' + Suffix[5];
          TruncTril := StrToInt64(RightStr(IntToStr(xD), 15));
          if (TruncTril <> 0) then
             BTStr := BTStr + ', ' + fTrilENG(TruncTril);
       end
       else
       if (xD >= 10000000000000000) and (xD <= 19999999999999999) then
       begin
          BTStr := Teens[StrToInt(LeftStr(IntToStr(xD), 2))] + ' ' + Suffix[5];
          TruncTril := StrToInt64(RightStr(IntToStr(xD), 15));
          if (TruncTril <> 0) then
             BTStr := BTStr + ', ' + fTrilENG(TruncTril);
       end
       else
       if (xD >= 20000000000000000) and (xD <= 99999999999999999) then
       begin
          if (StrToInt64(LeftStr(IntToStr(xD), 2)) = 0) then
             BTStr := Tens[StrToInt64(LeftStr(IntToStr(xD), 2))] + ' ' + Suffix[5]
          else
             BTStr := Tens[StrToInt64(LeftStr(IntToStr(xD), 2))] + ' ' +
                      Ones[StrToInt64(LeftStr(IntToStr(xD), 2))] + ' ' + Suffix[5];
          TruncTril := StrToInt64(RightStr(IntToStr(xD), 15));
          if (TruncTril <> 0) then
             BTStr := BTStr + ', ' + fTrilENG(TruncTril);
       end
       else
       if (xD >= 100000000000000000) and (xD <= 999999999999999999) then
       begin
          BTStr := fHundENG(StrToInt64(LeftStr(IntToStr(xD), 3))) + ' ' + Suffix[5];
          TruncTril := StrToInt64(RightStr(IntToStr(xD), 15));
          if (TruncTril <> 0) then
             BTStr := BTStr + ', ' + fTrilENG(TruncTril);
       end;;
       Result := BTStr;
    end; 
begin
   iD := abs(Trunc(D));
   if (iD >= 0) and (iD <= 99) then RStr := fTensENG(iD) else
   if (iD >= 100) and (iD <= 999) then Rstr := RStr + fHundENG(iD) else
   if (iD >= 1000) and (iD <= 999999) then RStr := RStr + fThouENG(iD) else
   if (iD >= 1000000) and (iD <= 999999999) then RStr := RStr + fMioENG(iD) else
   if (iD >= 1000000000) and (iD <= 999999999999) then RStr := RStr + fBioENG(iD) else
   if (iD >= 1000000000000) and (iD <= 999999999999999) then RStr := RStr + fTrilENG(iD) else
   if (iD >= 1000000000000000) and (iD <= 999999999999999999) then RStr := RStr + fQuadENG(iD); 
   if ChkER then RStr := RStr + ' ' + ER;
   vFrac := Frac(D);
   if (vFrac <> 0) then
   begin
      sDec := FormatFloat('0.000000', vFrac);
      if ChkER then
      begin
         sDec := MidStr(sDec, 3, 2);
         vDec := StrToInt(sDec);
         if (vDec > 0) then RStr := RStr + ' and ' + fTensENG(vDec) + ' cents.';
      end
      else
      begin
         RStr := RStr + ' point ';
         ReadFrac := False;
         sFrac := '';
         for I := Length(sDec) downto 3 do
         begin
            if (sDec[I] <> '0') then ReadFrac := True;
            if ReadFrac then sFrac := Ones[StrToInt(sDec[I])] + ' ' + sFrac;
         end;
         RStr := RStr + sFrac;
      end;
   end;
   Result := RStr;
end;

Open in new window


The Implementation

procedure TForm1.Button1Click(Sender: TObject); //Currency Format
begin
     Memo1.Clear;
     Memo1.Text := NTW(True, 'Dollar', 185012,345020);
     //Result : one hundred and eighty five thousand, and twelve dollar and thirty four cents
end;

procedure TForm1.Button2Click(Sender: TObject); //Math Format
begin
     Memo1.Clear;
     Memo1.Text := NTW(False, '', 185012,345020);
     //Result : one hundred and eighty five thousand, and twelve point three four five zero two
end;

Open in new window



Function Explanation
NTW(ChkER: Boolean; ER: String; D: Double): String;
ChkER = True for  Using Currency Format, False for math format
ER = Currency Name eg: 'Dollar'
D = Numeric Value

feature::
1. Support Multi Currency (Means you can input any currency name you want. ^^ )
2. Support Decimal until 6 Digit
3. Can be use for Math Number-To-Words Also.
 
May be for some advance usage you can modified this code as you need, and the last hope that this code can be really useful for your project.
3
Comment
Author:b10w01f
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
0 Comments

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Join & Write a Comment

In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Suggested Courses

Keep in touch with Experts Exchange

Tech news and trends delivered to your inbox every month