Win a copy of The Java Performance Companion this week in the Performance forum!

# Fractions

G. Graz
Ranch Hand
Posts: 30
I am trying to do simple conversion of a "long" fraction to it's Prime number

G. Graz
Ranch Hand
Posts: 30
Sorry hit the wrong button ! Ok, so if a fraction is 1/2 then it's good , if the fraction is 3/2 , the method will convert it to 1-1/2. Here is my code:

my "else if" is causing me all the problems ( convert 3/2 to 1-1/2). Any suggestions what I am doing wrong ? I am getting incompatible types when I try to compile, but I though my toString was taking care of that ? Thank you for the insight !! Suggestions always welcome !! Thank you!

Greg

karthikeyan Chockalingam
Ranch Hand
Posts: 259
In the else block you can either use
result = Double.toString((a/b));
or
result = ""+(a/b);

Note: a/b is primitive double which does not have a toString method.
[ May 15, 2008: Message edited by: karthi keyan ]

Campbell Ritchie
Sheriff
Posts: 49457
64
Try printing out a / b and a % b. That's all you need to do. You may end up with � coming out as 0 1/2, and ou will get peculiar results with negative numbers, but you can work out yourself how to deal with that!

Satya Maheshwari
Ranch Hand
Posts: 368
You can write something as below:

Campbell Ritchie
Sheriff
Posts: 49457
64
Originally posted by Satya Maheshwari:
You can write something as below:

That line won't work correctly, I am afraid.

Satya Maheshwari
Ranch Hand
Posts: 368
Originally posted by Campbell Ritchie:
That line won't work correctly, I am afraid.

Thanks for correcting. Very naive on my part . It should be

Campbell Ritchie
Sheriff
Posts: 49457
64
That will work, but I think this is better

. . . denominator < 0 ^ numerator < 0 . . .

Work it out. And you ought to throw an Exception if denominator is 0, before reaching that stage.

Satya Maheshwari
Ranch Hand
Posts: 368
Originally posted by Campbell Ritchie:
That will work, but I think this is better

. . . denominator < 0 ^ numerator < 0 . . .

Work it out. And you ought to throw an Exception if denominator is 0, before reaching that stage.

Yes I agree 'denominator is 0' case should be handled.
[ May 19, 2008: Message edited by: Satya Maheshwari ]