Articles   Members Online:
-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 currency value into a string 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
20-Oct-02
Category
Object Pascal-Strings
Language
Delphi 2.x
Views
76
User Rating
No Votes
# Votes
0
Replies
0
Publisher:
DSP, Administrator
Reference URL:
DKB
			Author: Jonas Bilinkevicius

How can I convert a value like $ 405.00 into a string?

Answer:

Solve 1:

I have written a function similar to what you want, but some modification need to 
be done. CurrToStrFunc(1234.56, 'Dollar', 'Cent');

1   unit CurrToStrProc;
2   
3   interface
4   
5   uses
6     SysUtils;
7   
8   function CurrToStrFunc(InputCur: Currency; InputDollar: string; InputCent: string):
9     string;
10  function Length_1(InputString: string): string;
11  function Length_2(InputString: string): string;
12  function Length_3(InputString: string): string;
13  function Length_4(InputString: string): string;
14  function Length_5(InputString: string): string;
15  function Length_6(InputString: string): string;
16  function Length_7(InputString: string): string;
17  function Length_8(InputString: string): string;
18  function Length_9(InputString: string): string;
19  
20  implementation
21  
22  function CurrToStrFunc(InputCur: Currency; InputDollar: string; InputCent: string):
23    string;
24  var
25    InputStr, DollarValue, DollarStr, CentValue, CentStr: string;
26    Counter, CentCounter: Integer;
27    CentFlag: Boolean;
28  begin
29    InputStr := CurrToStr(InputCur);
30    DollarValue := '';
31    DollarStr := '';
32    CentValue := '';
33    CentStr := '';
34    CentCounter := 0;
35    CentFlag := False;
36    for Counter := 1 to StrLen(PChar(InputStr)) do
37    begin
38      if (InputStr[Counter] <> '.') and (CentFlag = False) then
39        DollarValue := DollarValue + InputStr[Counter]
40      else
41      begin
42        if (InputStr[Counter] <> '.') then
43        begin
44          if (CentCounter < 2) then
45            CentValue := CentValue + InputStr[Counter];
46          CentCounter := CentCounter + 1;
47        end;
48        CentFlag := True;
49      end;
50    end;
51    if (CentCounter = 1) then
52      CentValue := CentValue + '0';
53    case StrLen(PChar(DollarValue)) of
54      0: DollarStr := '';
55      1: DollarStr := Length_1(DollarValue);
56      2: DollarStr := Length_2(DollarValue);
57      3: DollarStr := Length_3(DollarValue);
58      4: DollarStr := Length_4(DollarValue);
59      5: DollarStr := Length_5(DollarValue);
60      6: DollarStr := Length_6(DollarValue);
61      7: DollarStr := Length_7(DollarValue);
62      8: DollarStr := Length_8(DollarValue);
63      9: DollarStr := Length_9(DollarValue);
64    else
65      DollarStr := '?';
66    end;
67    if (CentFlag = True) then
68      CentStr := Length_2(CentValue);
69    if (InputDollar = '') then
70    begin
71      if (InputCent = '') then
72      begin
73        if (CentFlag = True) then
74          Result := DollarStr + ' ' + CentStr + ' ' + 'Only'
75        else
76          Result := DollarStr + ' ' + 'Only';
77      end
78      else if (CentFlag = True) then
79        Result := DollarStr + ' ' + InputCent + ' ' + CentStr + ' ' + 'Only'
80      else
81        Result := DollarStr + ' ' + 'Only';
82    end
83    else
84    begin
85      if (InputCent = '') then
86      begin
87        if (CentFlag = True) then
88          Result := InputDollar + ' ' + DollarStr + ' ' + CentStr + ' ' + 'Only'
89        else
90          Result := InputDollar + ' ' + DollarStr + ' ' + 'Only';
91      end
92      else if (CentFlag = True) then
93        Result := InputDollar + ' ' + DollarStr + ' ' + InputCent + ' ' + CentStr +
94   				' ' + 'Only'
95      else
96        Result := InputDollar + ' ' + DollarStr + ' ' + 'Only';
97    end;
98  end;
99  
100 function Length_1(InputString: string): string;
101 begin
102   case StrToInt(InputString) of
103     1: Result := 'One';
104     2: Result := 'Two';
105     3: Result := 'Three';
106     4: Result := 'Four';
107     5: Result := 'Five';
108     6: Result := 'Six';
109     7: Result := 'Seven';
110     8: Result := 'Eight';
111     9: Result := 'Nine';
112   end;
113 end;
114 
115 function Length_2(InputString: string): string;
116 begin
117   case StrToInt(InputString[1]) of
118     0:
119       begin
120         Result := Length_1(InputString[2])
121       end;
122     1:
123       begin
124         case StrToInt(InputString[2]) of
125           0: Result := 'Ten';
126           1: Result := 'Eleven';
127           2: Result := 'Twelve';
128           3: Result := 'Thirteen';
129           4: Result := 'Fourteen';
130           5: Result := 'Fiveteen';
131           6: Result := 'Sixteen';
132           7: Result := 'Seventeen';
133           8: Result := 'Eighteen';
134           9: Result := 'Nineteen';
135         end;
136       end;
137     2:
138       begin
139         if (InputString[2] = '0') then
140           Result := 'Twenty'
141         else
142           Result := 'Twenty' + ' ' + Length_1(InputString[2])
143       end;
144     3:
145       begin
146         if (InputString[2] = '0') then
147           Result := 'Thirty'
148         else
149           Result := 'Thirty' + ' ' + Length_1(InputString[2])
150       end;
151     4:
152       begin
153         if (InputString[2] = '0') then
154           Result := 'Fourty'
155         else
156           Result := 'Fourty' + ' ' + Length_1(InputString[2])
157       end;
158     5:
159       begin
160         if (InputString[2] = '0') then
161           Result := 'Fivety'
162         else
163           Result := 'Fivety' + ' ' + Length_1(InputString[2])
164       end;
165     6:
166       begin
167         if (InputString[2] = '0') then
168           Result := 'Sixty'
169         else
170           Result := 'Sixty' + ' ' + Length_1(InputString[2])
171       end;
172     7:
173       begin
174         if (InputString[2] = '0') then
175           Result := 'Seventy'
176         else
177           Result := 'Seventy' + ' ' + Length_1(InputString[2])
178       end;
179     8:
180       begin
181         if (InputString[2] = '0') then
182           Result := 'Eighty'
183         else
184           Result := 'Eighty' + ' ' + Length_1(InputString[2])
185       end;
186     9:
187       begin
188         if (InputString[2] = '0') then
189           Result := 'Ninety'
190         else
191           Result := 'Ninety' + ' ' + Length_1(InputString[2])
192       end;
193   end;
194 end;
195 
196 function Length_3(InputString: string): string;
197 begin
198   if (Copy(InputString, 1, 2) = '00') then
199     Result := Length_1(InputString[3])
200   else if (Copy(InputString, 2, 2) = '00') then
201     Result := Length_1(InputString[1]) + ' ' + 'Hundred'
202   else if (Copy(InputString, 1, 1) = '0') then
203     Result := Length_2(Copy(InputString, 2, 2))
204   else
205     Result := Length_1(InputString[1]) + ' ' + 'Hundred' + ' ' +
206       Length_2(Copy(InputString, 2, 2));
207 end;
208 
209 function Length_4(InputString: string): string;
210 begin
211   if (Copy(InputString, 2, 3) = '000') then
212     Result := Length_1(InputString[1]) + ' ' + 'Thousand'
213   else if (InputString[2] = '0') then
214     Result := Length_1(InputString[1]) + ' ' + 'Thousand' + ' ' +
215       Length_2(Copy(InputString, 3, 2))
216   else
217     Result := Length_1(InputString[1]) + ' ' + 'Thousand' + ' ' +
218       Length_1(InputString[2]) + ' ' + 'Hundred' + ' ' + Length_2(Copy(InputString, 
219 3,
220         2));
221 end;
222 
223 function Length_5(InputString: string): string;
224 begin
225   if (Copy(InputString, 2, 4) = '0000') then
226     Result := Length_2(Copy(InputString, 1, 2)) + ' ' + 'Thousand'
227   else if (InputString[3] = '0') then
228     Result := Length_2(Copy(InputString, 1, 2)) + ' ' + 'Thousand' + ' ' +
229       Length_2(Copy(InputString, 4, 2))
230   else
231     Result := Length_2(Copy(InputString, 1, 2)) + ' ' + 'Thousand' + ' ' +
232       Length_1(InputString[3]) + ' ' + 'Hundred' + ' ' + Length_2(Copy(InputString, 
233 4,
234         2));
235 end;
236 
237 function Length_6(InputString: string): string;
238 begin
239   if (Copy(InputString, 1, 3) = '000') then
240     Result := Length_3(Copy(InputString, 4, 3))
241   else if (Copy(InputString, 2, 5) = '00000') then
242     Result := Length_3(Copy(InputString, 1, 3)) + ' ' + 'Thousand'
243   else if (InputString[4] = '0') then
244     Result := Length_3(Copy(InputString, 1, 3)) + ' ' + 'Thousand' + ' ' +
245       Length_2(Copy(InputString, 5, 2))
246   else
247     Result := Length_3(Copy(InputString, 1, 3)) + ' ' + 'Thousand' + ' ' +
248       Length_1(InputString[4]) + ' ' + 'Hundred' + ' ' + Length_2(Copy(InputString, 
249 5,
250         2));
251 end;
252 
253 function Length_7(InputString: string): string;
254 begin
255   if (Copy(InputString, 2, 6) = '000000') then
256     Result := Length_1(InputString[1]) + ' ' + 'Million'
257   else if (Copy(InputString, 2, 3) = '000') then
258     Result := Length_1(InputString[1]) + ' ' + 'Million' + ' ' +
259       Length_3(Copy(InputString, 5, 3))
260   else
261     Result := Length_1(InputString[1]) + ' ' + 'Million' + ' ' +
262       Length_6(Copy(InputString, 2, 6));
263 end;
264 
265 function Length_8(InputString: string): string;
266 begin
267   if (Copy(InputString, 2, 7) = '0000000') then
268     Result := Length_2(Copy(InputString, 1, 2)) + ' ' + 'Million'
269   else if (Copy(InputString, 3, 3) = '000') then
270     Result := Length_2(Copy(InputString, 1, 2)) + ' ' + 'Million' + ' ' +
271       Length_3(Copy(InputString, 6, 3))
272   else
273     Result := Length_2(Copy(InputString, 1, 2)) + ' ' + 'Million' + ' ' +
274       Length_6(Copy(InputString, 3, 6));
275 end;
276 
277 function Length_9(InputString: string): string;
278 begin
279   if (Copy(InputString, 2, 8) = '00000000') then
280     Result := Length_3(Copy(InputString, 1, 3)) + ' ' + 'Million'
281   else if (Copy(InputString, 7, 3) = '000') then
282     Result := Length_3(Copy(InputString, 1, 3)) + ' ' + 'Million' + ' ' +
283       Length_3(Copy(InputString, 7, 3))
284   else
285     Result := Length_3(Copy(InputString, 1, 3)) + ' ' + 'Million' + ' ' +
286       Length_6(Copy(InputString, 4, 6));
287 end;
288 
289 end.



Solve 2:

290 CurrencyToString(const Value: Double): string;
291   function IntToEnglish(const Value: LongInt): string;
292 
293   implementation
294 
295 uses
296     Math;
297 
298 const
299     ENGLISH_ONES: array[1..19] of string = (
300       'one',
301       'two',
302       'three',
303       'four',
304       'five',
305       'six',
306       'seven',
307       'eight',
308       'nine',
309       'ten',
310       'eleven',
311       'twelve',
312       'thirteen',
313       'fourteen',
314       'fifteen',
315       'sixteen',
316       'seventeen',
317       'eighteen',
318       'nineteen');
319 
320     ENGLISH_TENS: array[1..9] of string = (
321       'ten',
322       'twenty',
323       'thirty',
324       'forty',
325       'fifty',
326       'sixty',
327       'seventy',
328       'eighty',
329       'ninety');
330 
331     ENGLISH_GROUP_SUFFIXES: array[1..3] of string = (
332       'thousand',
333       'million',
334       'billion');
335 
336   function CurrencyToString(const Value: Double): string;
337 var
338   Cents: LongInt;
339 begin
340   Cents := Round(Value * 100);
341   Result := IntToEnglish(Cents div 100) + ' and ' + IntToStr(Cents mod 100) +
342     '/100 dollars';
343 end;
344 
345 function IntToEnglish(const Value: LongInt): string;
346 var
347   GroupIndex: Integer;
348   GroupValue: Integer;
349 begin
350   if (Value = 0) then
351     Result := 'zero'
352   else if (Value < 0) then
353     Result := 'negative ' + IntToEnglish(-Value)
354   else
355   begin
356     Result := '';
357     for GroupIndex := (Trunc((8 * SizeOf(Value) - 1) / (3 * Ln(10) / Ln(2)))) 
358 downto 0
359       do
360     begin
361       GroupValue := Value div Round(IntPower(10, 3 * GroupIndex)) mod 1000;
362       if (GroupValue > 0) then
363       begin
364         if (GroupValue div 100 > 0) then
365           Result := Result + ENGLISH_ONES[GroupValue div 100] + ' hundred';
366         case (GroupValue mod 100) of
367           0: ;
368           1..19:
369             Result := Result + ENGLISH_ONES[GroupValue mod 100] + ' ';
370         else
371           begin
372             Result := Result + ENGLISH_TENS[GroupValue div 10 mod 10];
373             if (GroupValue mod 10 > 0) then
374               Result := Result + '-' + ENGLISH_ONES[GroupValue mod 10];
375             Result := Result + ' ';
376           end;
377         end;
378         if (GroupIndex > 0) then
379           Result := Result + ENGLISH_GROUP_SUFFIXES[GroupIndex] + ' ';
380       end;
381     end;
382     SetLength(Result, Length(Result) - 1); {Remove the trailing space}
383   end;
384 end;


Calling CurrencyToString(12345678.90) returns: 'twelve million three hundred forty-five thousand six hundred seventy-eight and 90/100 dollars'

			
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