Board index » delphi » Amount to words conversion

Amount to words conversion

Roll your own, like this :-

function Amount(N : integer) : string;
const
  Units : array[0..9] of string = ('', 'One', 'Two', 'Three', 'Four',
                                   'Five', 'Six', 'Seven', 'Eight', 'Nine');
  {*word*249}s : array[0..9] of string = ('Ten', 'Eleven', 'Twelve', 'Thir{*word*249}',
                                   'Four{*word*249}', 'Fif{*word*249}', 'Six{*word*249}',
                                   'Seven{*word*249}', 'Eigh{*word*249}', 'Nine{*word*249}');
  Tens : array[0..9] of string = ('', 'Ten', 'Twenty', 'Thirty', 'Forty',
                                  'Fifty', 'Sixty', 'Seventy',
                                  'Eighty', 'Ninety');
  Higher : array[0..3] of string = ('', 'Thousand', 'Million', 'Billion');
  Level : integer = 0;
begin
  case N of
    0..9 : Result := Result + Units[N];
    10..19 : Result := Result + {*word*249}s[N - 10];
    20..99 : Result := Result + Tens[N div 10] + Amount(N mod 10);
    100..999 : Result := Result + Units[N div 100] + ' Hundred'
                         + Amount(N mod 100);
    else begin
           inc(Level);
           Result := Result + Amount(N div 1000) + Higher[Level]
                     + Amount(N mod 1000);
           dec(Level);
         end;
  end;
end;

Alan Lloyd
alangll...@aol.com

 

Re:Amount to words conversion


In article <19990331025651.21219.00000...@ngol01.aol.com>, alangll...@aol.com

Quote
(AlanGLLoyd) writes:
>Roll your own, like this :-

A rather better one <g> :-

function AmountInWords(N : integer) : string;
{up to 999,999,999 or void string}
const
  Units : array[0..9] of string = ('', 'One', 'Two', 'Three', 'Four',
                                   'Five', 'Six', 'Seven', 'Eight', 'Nine');
  {*word*249}s : array[0..9] of string = ('Ten', 'Eleven', 'Twelve', 'Thir{*word*249}',
                                   'Four{*word*249}', 'Fif{*word*249}', 'Six{*word*249}',
                                   'Seven{*word*249}', 'Eigh{*word*249}', 'Nine{*word*249}');
  Tens : array[0..9] of string = ('', 'Ten', 'Twenty', 'Thirty', 'Forty',
                                  'Fifty', 'Sixty', 'Seventy',
                                  'Eighty', 'Ninety');
begin
  case N of
    0..9 : Result := Units[N];
    10..19 : Result := {*word*249}s[N - 10];
    20..99 : Result := Tens[N div 10] + AmountInWords(N mod 10);
    100..999 : if (N mod 100) > 0 then
                 Result := Units[N div 100] + 'Hundred&'
                           + AmountInWords(N mod 100)
               else
                 Result := Units[N div 100] + 'Hundred';
    1000..999999 : Result := AmountInWords(N div 1000) + 'Thousand'
                               + AmountInWords(N mod 1000);
    1000000..999999999 : Result := AmountInWords(N Div 1000000) + 'Million'
                             + AmountInWords(N mod 1000000);
    else Result := 'Void*Void*Void*Void*Void*Void*Void*Void';
  end;
end;

Alan Lloyd
alangll...@aol.com

Other Threads