File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes fraction calculation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "fraction calculation" Watch "fraction calculation" New topic
Author

fraction calculation

glen li
Greenhorn

Joined: Aug 24, 2004
Posts: 19
Hello,

Can I do this type of calculation in Java like 3/8*5/9=5/24? Or I must define the operation myself using like "BigInteger"? Thanks!

glen
shandilya popuru
Ranch Hand

Joined: Dec 21, 2004
Posts: 95
yup u can do such comparisions in java directly



sandy
Mark Vedder
Ranch Hand

Joined: Dec 17, 2003
Posts: 624

shandilya is correct that you can directly to the math 3/8*5/9, however, depending on how you set it up (i.e. what type of primary your answer variable is set up as), you will end up either with a truncated integer (int) of zero, or a floating point number (i.e. a decimal number) of 0.06944444. (And no need to use BigInteger).

If you want your answer to be a fraction, you would need to create a Fraction class. Define it with two int's as instance variables to represent the numerator & denominator. Then create various methods for adding, subtracting, multiplying, etc. You would also probably want a reduce method, and perhaps a findCommonDenominator method. All the things you need to do true fraction math.

Then override Object's toString method to output the result as a fraction.
[ January 15, 2005: Message edited by: Mark Vedder ]
glen li
Greenhorn

Joined: Aug 24, 2004
Posts: 19
Thanks guys for making it clear!

glen
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: fraction calculation