Sumber Kode

Numbers to Words - 1 to one; 2 to two; 22 to twenty-two; etc.

  • delphibasics
  • Nov 19' 14
  • Delphi
  • 80
  • 294
  • United States
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
 
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
154:
155:
156:
157:
158:
159:
160:
161:
162:
163:
164:
165:
166:
167:
168:
169:
170:
171:
172:
173:
174:
175:
176:
177:
178:
179:
180:
181:
182:
183:
184:
185:
186:
187:
188:
189:
190:
191:
192:
193:
194:
195:
196:
197:
198:
199:
200:
201:
202:
203:
204:
205:
206:
207:
208:
209:
210:
211:
212:
213:
214:
215:
216:
217:
218:
219:
220:
221:
222:
223:
224:
225:
226:
227:
228:
229:
230:
231:
232:
233:
234:
235:
236:
237:
238:
239:
240:
241:
242:
243:
244:
245:
246:
247:
248:
249:
250:
251:
252:
253:
254:
255:
256:
257:
258:
259:
260:
261:
262:
263:
264:
265:
266:
267:
268:
269:
270:
271:
272:
273:
274:
275:
276:
277:
278:
279:
280:
281:
282:
283:
284:
285:
286:
287:
288:
289:
290:
291:
292:
293:
294:
295:
296:
297:
298:
299:
300:
301:
302:
303:
304:
305:
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;
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;

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.

by b10w01f
 
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.
Powered by
Pasar Kode Partner

Share

  • Tags :
comments powered by Disqus

Kode Terkait

How to turn the monitor on and off

How to get the file type description for a file

Store more than 64 KB in a TRichEdit

Introduction to using DLLs in Delphi

Multiple Document Interface [MDI] Example

Owner Draw - ComboBox

How to allow or disallow program shutdown in Delphi

Delete the relocations from a PE file.

Owner Draw - DB Grid

Low Level Utilities - Resource Package



Kategori