Articles   Members Online: 3
-Article/Tip Search
-News Group Search over 21 Million news group articles.
-Delphi/Pascal
-CBuilder/C++
-C#Builder/C#
-JBuilder/Java
-Kylix
Member Area
-Home
-Account Center
-Top 10 NEW!!
-Submit Article/Tip
-Forums Upgraded!!
-My Articles
-Edit Information
-Login/Logout
-Become a Member
-Why sign up!
-Newsletter
-Chat Online!
-Indexes NEW!!
Employment
-Build your resume
-Find a job
-Post a job
-Resume Search
Contacts
-Contacts
-Feedbacks
-Link to us
-Privacy/Disclaimer
Embarcadero
Visit Embarcadero
Embarcadero Community
JEDI
Links
How to convert a number to text Turn on/off line numbers in source code. Switch to Orginial background IDE or DSP color Comment or reply to this aritlce/tip for discussion. Bookmark this article to my favorite article(s). Print this article
21-Jan-03
Category
Object Pascal-Strings
Language
Delphi All Versions
Views
88
User Rating
No Votes
# Votes
0
Replies
0
Publisher:
DSP, Administrator
Reference URL:
DKB
			Author: William Gerbert

How to convert a number to text?

Answer:

Here a code to covert a Number (Real) to string:

1   function RealToTxt(Amount: Real): string;
2   var
3     Num: LongInt;
4     Fracture: Integer;
5   
6     function Num2Str(Num: LongInt): string;
7     const
8       hundred = 100;
9       thousand = 1000;
10      million = 1000000;
11      billion = 1000000000;
12    begin
13      if Num >= billion then
14        if (Num mod billion) = 0 then
15          Num2Str := Num2Str(Num div billion) + ' Billion'
16        else
17          Num2Str := Num2Str(Num div billion) + ' Billion ' +
18            Num2Str(Num mod billion)
19      else if Num >= million then
20        if (Num mod million) = 0 then
21          Num2Str := Num2Str(Num div million) + ' Million'
22        else
23          Num2Str := Num2Str(Num div million) + ' Million ' +
24            Num2Str(Num mod million)
25      else if Num >= thousand then
26        if (Num mod thousand) = 0 then
27          Num2Str := Num2Str(Num div thousand) + ' Thousand'
28        else
29          Num2Str := Num2Str(Num div thousand) + ' Thousand ' +
30            Num2Str(Num mod thousand)
31      else if Num >= hundred then
32        if (Num mod hundred) = 0 then
33          Num2Str := Num2Str(Num div hundred) + ' Hundred'
34        else
35          Num2Str := Num2Str(Num div hundred) + ' Hundred ' +
36            Num2Str(Num mod hundred)
37      else
38        case (Num div 10) of
39          6, 7, 9: if (Num mod 10) = 0 then
40              Num2Str := Num2Str(Num div 10) + 'ty'
41            else
42              Num2Str := Num2Str(Num div 10) + 'ty-' +
43                Num2Str(Num mod 10);
44          8: if Num = 80 then
45              Num2Str := 'Eighty'
46            else
47              Num2Str := 'Eighty-' + Num2Str(Num mod 10);
48          5: if Num = 50 then
49              Num2Str := 'Fifty'
50            else
51              Num2Str := 'Fifty-' + Num2Str(Num mod 10);
52          4: if Num = 40 then
53              Num2Str := 'Forty'
54            else
55              Num2Str := 'Forty-' + Num2Str(Num mod 10);
56          3: if Num = 30 then
57              Num2Str := 'Thirty'
58            else
59              Num2Str := 'Thirty-' + Num2Str(Num mod 10);
60          2: if Num = 20 then
61              Num2Str := 'Twenty'
62            else
63              Num2Str := 'Twenty-' + Num2Str(Num mod 10);
64          0, 1: case Num of
65              0: Num2Str := 'Zero';
66              1: Num2Str := 'One';
67              2: Num2Str := 'Two';
68              3: Num2Str := 'Three';
69              4: Num2Str := 'Four';
70              5: Num2Str := 'Five';
71              6: Num2Str := 'Six';
72              7: Num2Str := 'Seven';
73              8: Num2Str := 'Eight';
74              9: Num2Str := 'Nine';
75              10: Num2Str := 'Ten';
76              11: Num2Str := 'Eleven';
77              12: Num2Str := 'Twelve';
78              13: Num2Str := 'Thirteen';
79              14: Num2Str := 'Fourteen';
80              15: Num2Str := 'Fifteen';
81              16: Num2Str := 'Sixteen';
82              17: Num2Str := 'Seventeen';
83              18: Num2Str := 'Eightteen';
84              19: Num2Str := 'Nineteen'
85            end
86        end
87    end {Num2Str};
88  
89  begin
90    Num := Trunc(Amount);
91    Fracture := Round(1000 * Frac(Amount));
92    if Num > 0 then
93      Result := Num2Str(Num) + ' and ';
94    if Fracture > 0 then
95      Result := Result + IntToStr(Fracture) + '/1000'
96    else
97      Result := Result + '000/1000';
98  end;
99  
100 procedure TForm1.Button1Click(Sender: TObject);
101 begin
102   form1.Caption := realtotxt(123);
103 end;


			
Vote: How useful do you find this Article/Tip?
Bad Excellent
1 2 3 4 5 6 7 8 9 10

 

Advertisement
Share this page
Advertisement
Download from Google

Copyright © Mendozi Enterprises LLC