Title: Convert numbers to words.
Question: 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 Result := Result + sSpell[iInt];
Dec(iInt, iInt);
end;
if (iInt 9) and (iInt 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 i := StrToInt(Copy(IntToStr(iInt),0,1));
Result := Result + sSpell[i] + ' Hundred(s)';
Dec(iInt, i*100);
end;
if (iInt 999) and (iInt i := StrToInt(Copy(IntToStr(iInt),0,1));
Result := Result + sSpell[i] + 'Thousand(s)';
Dec(iInt, i*1000);
end;
if (iInt 9999) and (iInt i := StrToInt(Copy(IntToStr(iInt), 0, 2));
Result := Result + sSpell[i] + ' Thousand(s)';
Dec(iInt, i*1000);
end;
if (iInt 99999) and (iInt i := StrToInt(Copy(IntToStr(iInt), 0, 1));
Result := Result + sSpell[i] + ' Hundred(s)';
Dec(iInt, i*100000);
end;
if (iInt 999999) and (iInt i := StrToInt(Copy(IntToStr(iInt), 0, 1));
Result := Result + sSpell[i] + ' Million(s) ';
Dec(iInt, i*1000000);
end;
if (iInt 9999999) and (iInt 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 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 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');