Mega Code Archive

 
Categories / Delphi / Examples
 

Strtomoney

This is a Delphi unit that I use. I can't remember where I got it from, but Andy Preston wrote it back in '99 Hope this helps.. Bruce H. Perth, Australia Unit NumWords; { Andy Preston, Apollo Developments andy@apollod.freeserve.co.uk } { NumAsWords - Version 3.2 } { Copyright (C) 1999 Andy Preston } { Object-Pascal Function to express an integer as words in the English Language } { This unit is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public } { License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later } { version. } { This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty } { of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more details. } { You should have received a copy of the GNU Library General Public License along with this library; if not, write to the } { Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. } Interface Function NumberInWords (TheNumber : Integer) : String; Function EMoneyInWords (TheAmount : Extended) : String; Function IMoneyInWords (Major, Minor : Integer) : String; { Major in Pounds/Dollars etc, Minor in Pence/Cents etc } Implementation { change this to point at pound.res for u.k. use, leave as dollar.res for everywhere else } {$R dollar.res} Uses SysUtils; Function DoDigit (Digit : Integer) : String; Begin Case Digit Of 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 DoTriplet (TheNumber : Integer; AndNeeded : Boolean) : String; Var Digit, Num : Integer; Begin Result:=''; Num:=TheNumber Mod 100; If (Num>10)And (Num<20)Then Begin Case Num Of 11 : Result:='eleven'; 12 : Result:='twelve'; 13 : Result:='thirteen'; 14 : Result:='fourteen'; 15 : Result:='fifteen'; 16 : Result:='sixteen'; 17 : Result:='seventeen'; 18 : Result:='eightteen'; 19 : Result:='nineteen'; End; Num:=TheNumber Div 100; End Else Begin Num:=TheNumber; Digit:=Num Mod 10; Num:=Num Div 10; If Digit>0 Then Result:=DoDigit (Digit); Digit:=Num Mod 10; Num:=Num Div 10; If Digit>0 Then Begin Case Digit Of 1 : Result:='ten'; 2 : Result:='twenty '+Result; 3 : Result:='thirty '+Result; 4 : Result:='fourty '+Result; 5 : Result:='fifty '+Result; 6 : Result:='sixty '+Result; 7 : Result:='seventy '+Result; 8 : Result:='eighty '+Result; 9 : Result:='ninety '+Result; End; End; Result:=Trim (Result); End; Digit:=Num Mod 10; If (Result<>'')And (AndNeeded And (Digit>0))Then Result:='and '+Result; If Digit>0 Then Result:=DoDigit (Digit)+' hundred '+Result; Result:=Trim (Result); End; Function NumberInWords (TheNumber : Integer) : String; Var Num, Triplet, Pass : Integer; Begin Result:=''; Num:=TheNumber; If Num>999999999 Then Raise Exception.Create ('Can''t express more than 999,999,999 in words'); For Pass:=1 To 3 Do Begin Triplet:=Num Mod 1000; Num:=Num Div 1000; If Triplet>0 Then Begin // If (Pass>1)And (Result<>'')Then Result:=', '+Result; If (Pass>1)And (Result<>'')Then Result:=' '+Result; Case Pass Of 2 : Result:=' thousand'+Result; 3 : Result:=' million'+Result; End; Result:=Trim (DoTriplet (Triplet, (Pass=1))+Result); End; End; End; Function EMoneyInWords (TheAmount : Extended) : String; Var AsString : String; P : Integer; Begin // It would seem logical at first to do something like this: // Major:=Int(TheAmount); // Result:=IMoneyInWords (Trunc (Major), Trunc (Round((TheAmount-Major)*100.0))); // (Under Delphi 2.0 at least) you get loads of very small (but significant) floating point errors. // If I'm being stupid, please e-mail and tell me so. But in the meantime here's this: AsString:=FormatFloat ('0.00', TheAmount); P:=Pos ('.', AsString); Result:=IMoneyInWords (StrToInt (Copy (AsString, 1, P-1)), StrToInt (Copy (AsString, P+1, Length (AsString)-P))); End; Function IMoneyInWords (Major, Minor : Integer) : String; Begin If Major=0 Then Result:='no' Else Result:=NumberInWords (Major); Result:=Result+' '+LoadStr (61000); If Minor=0 Then Result:=Result+' only' Else Result:=Result+' and '+DoTriplet (Minor, False)+' '+LoadStr (61001); End; End.