Dear all,

 Just wrote this for fun. It may help some one.

Best regards,
Sabu M H

(*********************************************************************************

  Unit name: UDigitConverter.pas

  Purpose:
  TDigitToWordConverter class can be used for
converting digits to words.
  Example : 12345 can be converted to 'Twelve Thousand
Three Hundred and Forty Five'

  Contact:
  Developed by Sabu M H
  E-mail : [EMAIL PROTECTED](comments and
suggestions can be sent to this id)

  Limitation:
  Only numbers upto 99999999 can be converted to
words.
  Indian numbering system is used (India, Pakistan,
Bangladesh and Myanmar(Burma))

  Comments:  
  Can be extended.
  The suffix words can be customized by changing the
constant values.

  Usage:
  1.Add UDigitConverter in uses clause.
  2.Create an instance of TDigitToWordConverter
  3.Assign the number to convert to 'NumberToConvert'
property.
  4.Call 'ConvertToWords' method.
  5.Read 'NumberInWords' property.

  Method used:
  Number is moved to an integer array.
  Find out the category.
  Convert to words.

  History:
  1.Date of development : 15-November-2005.

  2.Updated on : 26-June-2006
  Added string array constants and new functions for
clarity.

*************************************************************************************)

unit UDigitConverter;

interface

uses
  Windows, SysUtils;

const
  Ones: array[0..9] of string =
  (' Zero', ' One', ' Two', ' Three', ' Four', '
Five', ' Six', ' Seven', ' Eight', ' Nine');

  LessThanTwenty: 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');

  THOUSAND  = 'Thousand';
  HUNDRED   = 'Hundred';
  LAKH      = 'Lakh';
  CRORE     = 'Crore';
  CONNCETOR = 'and';

type
  TNumberType = (ntone, ntten, nthundred, ntthousand,
nttenthousand, ntlakh, nttenlakh, ntCrore);

  TDigitToWordConverter = class
  private
     NumberArray : array[0..7] of Integer;
     FNumberToConvert : Integer;
     FNumberInWords : String;

     function GetFirstTwo(Index:Integer):String;

     function GetOnes : String;
     function GetTens : String;
     function GetHundred : String;
     function GetThousand : String;
     function GetTenThousand : String;
     function GetLakh : String;
     function GetTenLakh : String;
     function GetCrore : String;
  public
     function ConvertToWords : String;

     property NumberToConvert : Integer read
FNumberToConvert write FNumberToConvert;
     property NumberInWords : String read
FNumberInWords; {only reading is permitted}
  end;

implementation

function
TDigitToWordConverter.GetFirstTwo(Index:Integer):String;
begin
  if (NumberArray[Index] = 1) then
    Result := LessThanTwenty[10 * NumberArray[Index] +
NumberArray[Index-1]]
  else
  begin
    Result := Tens[NumberArray[Index]];
    if (NumberArray[Index-1] <> 0) then
      Result := Result + Ones[NumberArray[Index-1]];
  end;
end;

function TDigitToWordConverter.GetOnes: String;
begin
  Result := Ones[NumberArray[ord(ntOne)]];
end;

function TDigitToWordConverter.GetTens: String;
begin
  if NumberArray[Ord(ntten)] = 1 then
    Result := LessThanTwenty[10 *
NumberArray[Ord(ntten)] + NumberArray[Ord(ntone)]];

  if NumberArray[Ord(ntten)] > 1 then
  begin
    Result := Tens[NumberArray[Ord(ntten)]];
    if NumberArray[Ord(ntOne)] > 0 then
      Result := Result + GetOnes;
  end;

  if NumberArray[Ord(ntten)] = 0 then
    if NumberArray[Ord(ntOne)] > 0 then
      Result := Result + GetOnes;
end;

function TDigitToWordConverter.GetHundred: String;
begin
  if (NumberArray[Ord(nthundred)] <> 0) then
    Result := Result +
Ones[NumberArray[Ord(nthundred)]] + ' ' + HUNDRED;

  if (10 * NumberArray[Ord(ntten)] +
NumberArray[Ord(ntone)]) > 0 then
    Result := Result + ' ' + CONNCETOR + GetTens;
end;

function TDigitToWordConverter.GetThousand:String;
begin
  if (NumberArray[Ord(ntthousand)] <> 0) then
    Result := Ones[NumberArray[Ord(ntthousand)]]+ ' '
+ THOUSAND;
  Result := Result + GetHundred;
end;

function TDigitToWordConverter.GetTenThousand:String;
begin
  if (NumberArray[Ord(nttenthousand)] <> 0) then
    Result := GetFirstTwo(Ord(nttenthousand)) + ' ' +
THOUSAND;
  If Result ='' then
    Result := Result + GetThousand
  else
    Result := Result + GetHundred;
end;

function TDigitToWordConverter.GetLakh:String;
begin
  if (NumberArray[Ord(ntlakh)] <> 0) then
    Result := Ones[NumberArray[Ord(ntlakh)]] + ' ' +
LAKH;
  Result := Result + GetTenThousand;
end;

function TDigitToWordConverter.GetTenLakh : String;
begin
  if (NumberArray[Ord(nttenlakh)] <> 0) then
    Result := GetFirstTwo(Ord(nttenlakh)) + ' ' +
LAKH;
  if Result = '' then
    Result := Result + GetLakh
  else
    Result := Result + GetTenThousand;
end;

function TDigitToWordConverter.GetCrore:String;
begin
  if (NumberArray[Ord(ntCrore)] <> 0) then
    Result := Ones[NumberArray[Ord(ntCrore)]] + ' '+
CRORE;
  Result := Result + GetTenLakh;
end;

function TDigitToWordConverter.ConvertToWords: String;
var
  NumberType : TNumberType;
  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 := Ord(ntone) to Ord(ntCrore) 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 := GetOnes;
    ntten         :     FNumberInWords := GetTens;
    nthundred     :     FNumberInWords := GetHundred;
    ntthousand    :     FNumberInWords := GetThousand;
    nttenthousand :     FNumberInWords := GetTenThousand;
    ntlakh            :         FNumberInWords := GetLakh;
    nttenlakh     :     FNumberInWords := GetTenLakh;
    ntCrore         :   FNumberInWords := GetCrore;
  end; 
end;

end.




__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 


------------------------ Yahoo! Groups Sponsor --------------------~--> 
Something is new at Yahoo! Groups.  Check out the enhanced email design.
http://us.click.yahoo.com/SISQkA/gOaOAA/yQLSAA/i7folB/TM
--------------------------------------------------------------------~-> 

-----------------------------------------------------
Home page: http://groups.yahoo.com/group/delphi-en/
To unsubscribe: [EMAIL PROTECTED] 
Yahoo! Groups Links

<*> To visit your group on the web, go to:
    http://groups.yahoo.com/group/delphi-en/

<*> To unsubscribe from this group, send an email to:
    [EMAIL PROTECTED]

<*> Your use of Yahoo! Groups is subject to:
    http://docs.yahoo.com/info/terms/
 


Reply via email to