Delphi32.com - Home!
| Home/News | Downloads | Forums | D32 Magazine | Resources | Info and Facts |  
 
 Getting the Lowest common denominator.


How do I convert two numbers representing a ratio to a string that represents a fraction of the lowest common denominator?   

    The following example demonstrates a technique based on equations originally solved by Euclid in approx. 300 B.C. :

    Example:

    function GetFracStr(Num : integer; Num2 : integer) : string; var N : integer; N2 : integer; Tmp : integer; begin if Num < Num2 then begin N := Num ; N2 := Num2; end else begin N := Num2; N2 := Num ; end; Tmp := N2 mod N ; while Tmp <> 0 do begin N2 := N ; N := Tmp; Tmp := N2 mod N ; end; result := IntToStr(Num div N ) + '/' + IntToStr(Num2 div N ); end;

    procedure TForm .Button Click(Sender: TObject); begin Memo .Lines.Add(GetFracStr(2, 64)); end; 7/ 6/98 4:3 :28 PM

     



  << Previous Faq     Complete List     Next Faq >>  



 
 Hits/month  2,500,000+ 
 Downloads
 (Since May 2000)
 7,393,709 
 Total Files  6,023 
 Forum msgs  7,670 
 Articles/FAQs  70+/900+ 
Kylix
Tips n Tricks
FAQs
Knowledge Base
Bug Listings
Articles
Books
Newsgroups
Links
Submissions
Testimonials
Advertising
Contact Us
About Us
Search Amazon:
Top Selling Software at Amazon

| Home/News | Downloads | Forums | Resources | Info and Facts | Testimonials |
  Site Search:
 


Comments/Problems: Webmaster@delphi32.com
Copyright © 1998-2006, Delphi32.com. All rights reserved.
Terms of Use