Important alert: (current site time 7/16/2013 5:48:32 AM EDT)
 

VB icon

Conversion of digits to words

Email
Submitted on: 11/15/2005 11:50:43 PM
By: Sabu M H  
Level: Advanced
User Rating: By 2 Users
Compatibility: Delphi 7
Views: 11744
(About the author)
 
     Using an instance of this class you can convert digits to words. for eg: 289 will be converted and shown as 'Two hundred and eighty nine' The largest number able to convert using this is 99999999.
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
				
!**************************************
! Name: Conversion of digits to words
! Description:Using an instance of this class you can convert digits to words. for eg: 289 will be converted and shown as 'Two hundred and eighty nine'
The largest number able to convert using this is 99999999.
! By: Sabu M H
!
! Inputs:We have to give the integer to be converted as words.
!
! Returns:String which will contain the words [converted from digits].
!
! Assumes:Should know only about the following three :
1. NumberToConvert property where the number is assigned.
2.To call ConvertNumberToDigits method to do the conversion.
3.To read the NumberInWords property to get the result.
!
! Side Effects:Limitation :
The largest number able to convert using this is 99999999.
!
!This code is copyrighted and has! limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=1668&lngWId=7!for details.!**************************************

(*
 TDigitToWordConverter class can be used for converting digits to words.
 The words can be customized by chaning the string constant values.
 Developed by Sabu M H
 E-mail : sabumh@rediffmail.com, sabumh@yahoo.com (comments and suggestions can be sent to these ids)
 Numbers till 1 crore can be converted to words.
 Indian way of conversion is used.
 Usage :
 1.Create an instance of TDigitToWordConverter
 2.Assign the number to convert to NumberToConvert property.
 3.Call ConvertNumberToDigits method.
 4.Read NumberInWords property.
 Method used :
 Number is moved to an integer array.
 Find out the category.
 Convert to words.
 Date : 15-November-2005.
*)
unit UDigitConverter;
interface
uses Windows,SysUtils,Controls;
const
 THOUSAND = 'thousand';
 HUNDRED= 'hundred';
 LAKH = 'lakh';
 CRORE = 'crore';
 CONNCETOR = 'and';
{Texts can be changed in GetOneText, GetTenText and GetLessThanTwenty functions also}
type
 TNumberType = (ntone, ntten, nthundred, ntthousand, nttenthousand, ntlakh, nttenlakh, ntCrore);
 TDigitToWordConverter = class
 private
FNumberToConvert : Integer;
NumberType : TNumberType;
NumberArray : array[0..7] of integer; {till crore}
FNumberInWords : String;
function GetOneText(aNumber : Integer) : String;
function GetLessThanTwenty(aNumber : Integer) : String;
function GetTenText(aNumber : Integer) : String;
function GetTenThousand:String;
function GetLakh:String;
function GetTenLakh : String;
function GetCrore:String;
Function LastThreeDigits: String;
 public
Function ConvertNumberToDigits : String;
property NumberToConvert : Integer read FNumberToConvert write FNumberToConvert;
property NumberInWords : String read FNumberInWords; {only reading is permitted}
end;
implementation
function TDigitToWordConverter.GetOneText(aNumber : Integer) : String;
{To retun number between 0 and 9}
begin
 case aNumber of
0 : Result := ' Zero';
1 : Result := ' One';
2 : Result := ' Two';
3 : Result := ' Three';
4 : Result := ' Four';
5 : Result := ' Five';
6 : Result := ' Six';
7 : Result := ' Seven';
8 : Result := ' Eight';
9 : Result := ' Nine';
 end;
end;
function TDigitToWordConverter.GetTenText(aNumber : Integer) : String;
{To retun numbers 20,30,40,50,60,70,80 and 90}
begin
 Result := '';
 case aNumber of
2 : Result := ' Twenty';
3 : Result := ' Thirty';
4 : Result := ' Forty';
5 : Result := ' Fifty';
6 : Result := ' Sixty';
7 : Result := ' Seventy';
8 : Result := ' Eighty';
9 : Result := ' Ninety';
 end;
end;
function TDigitToWordConverter.GetLessThanTwenty(aNumber : Integer) : String;
{To retun number between 10 and 19}
begin
 Result := '';
 case aNumber of
10 : Result := ' Ten';
11 : Result := ' Eleven';
12 : Result := ' Twelve';
13 : Result := ' Thirteen';
14 : Result := ' Fourteen';
15 : Result := ' Fifteen';
16 : Result := ' Sixteen';
17 : Result := ' Seventeen';
18 : Result := ' Eighteen';
19 : Result := ' Nineteen';
 end;
end;
Function TDigitToWordConverter.LastThreeDigits: String;
{To convert number more than 1 lakh and less than 999999}
begin
 if (NumberArray[2] <> 0) then
Result := Result + GetOneText(NumberArray[2])+' '+HUNDRED;
 if (NumberArray[1] <> 0) or (NumberArray[0] <> 0) then
 begin
if NumberArray[1] = 1 then
begin
 if NumberType = ntten then
Result := GetLessThanTwenty(10*NumberArray[1] + NumberArray[0])
 else
Result := Result +' '+CONNCETOR+GetLessThanTwenty(10*NumberArray[1] + NumberArray[0])
end
else
begin
 if NumberArray[1] > 1 then
 begin
if NumberType = ntten then
 Result := GetTenText(NumberArray[1])
else
 Result := Result +' '+CONNCETOR+GetTenText(NumberArray[1]);
 end;
 if NumberType <> ntone then
if NumberArray[1] = 0 then
 Result := Result +' '+CONNCETOR;
 if NumberArray[0] > 0 then
Result := Result + GetOneText(NumberArray[0])
end;
 end {if (NumberArray[1] <> 0) or (NumberArray[0] <> 0) then}
 else
if NumberType = ntone then
 Result := Result + GetOneText(NumberArray[0]);
end;
function TDigitToWordConverter.GetLakh:String;
{To convert number up to 999}
begin
 Result := GetOneText(NumberArray[5])+' '+ LAKH + GetTenThousand;
end;
function TDigitToWordConverter.GetTenThousand:String;
{To convert number more than 9999 and less than 1 lakh}
begin
 if (NumberArray[4] = 1) then
Result := GetLessThanTwenty(10*NumberArray[4] + NumberArray[3])
 else
 begin
Result := GetTenText(NumberArray[4]);
if (NumberArray[3] <> 0) then
Result := Result + GetOneText(NumberArray[3]);
 end;
 if Result <> '' then
Result := Result + ' '+THOUSAND;
 Result := Result + LastThreeDigits;
end;
function TDigitToWordConverter.GetTenLakh : String;
{To convert number more than 999999 and less than 1 crore}
begin
 if (NumberArray[6] = 1) then
Result := GetLessThanTwenty(10*NumberArray[6] + NumberArray[5]) +' '+ LAKH + GetTenThousand
 else
 begin
Result := GetTenText(NumberArray[6]);
if (NumberArray[5] <> 0) then
Result := Result + GetOneText(NumberArray[5]);
if Result <> '' then
 Result := Result +' '+ LAKH;
Result := Result + GetTenThousand;
 end;
end;
function TDigitToWordConverter.GetCrore:String;
{To convert number in crore}
begin
 Result := GetOneText(NumberArray[7]) + ' '+ CRORE + GetTenLakh;
end;
function TDigitToWordConverter.ConvertNumberToDigits: String;
{This function will convert the number assigned in FNumberToConvert to words.
The string will be kept in FNumberInWords}
var
 NumStr : String;
 i,j : Integer; {for looping}
begin
 FNumberInWords := ' ';
 
 NumStr := IntToStr(FNumberToConvert);
 {determining the number type}
 NumberType := TNumberType(Length(NumStr)-1);
 if NumberType > ntCrore then
 begin
MessageBox(0, 'Maximum number to convert : 99999999', 'Converter', MB_OK);
Exit;
 end;
 {intitializing the number array}
 for i := 0 to 7 do
NumberArray[i] := 0;
 {storing number to number array}
 j := 0;
 for i := Length(NumStr) downto 1 do
 begin
NumberArray[j] := StrToInt(NumStr[i]);
Inc(j);
 end;
 Case NumberType of
ntone : 	FNumberInWords := LastThreeDigits;
ntten : 	FNumberInWords := LastThreeDigits;
nthundred 		: 	FNumberInWords := LastThreeDigits;
ntthousand 		: 	FNumberInWords := GetTenThousand;
nttenthousand : 	FNumberInWords := GetTenThousand;
ntlakh 				: 	FNumberInWords := GetLakh;
nttenlakh 		: 	FNumberInWords := GetTenLakh;
ntCrore 			: 	FNumberInWords := GetCrore;
 end; {case}
end;
end.


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Advanced category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments

11/19/2005 5:52:46 PMFrancisco Vazquez

Thank you Sabu, I have done the changes you suggested and now it works very good. Thank you for the code and the help.
(If this comment was disrespectful, please report it.)

 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.