d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Java Decimal To Fraction?
123Next
Add Reply New Topic New Poll
Member
Posts: 13,042
Joined: Apr 12 2008
Gold: 0.00
Nov 7 2014 06:56pm
So, I'm working on a project right now, and I'm confused on how you would convert a decimal value (say .333333) to a fraction(1/3)

I've looked it up for a while on Google and haven't really been able to get a straight forward answer that I really understand...

Is there a simple way of doing this?


Example for the different situations I would need to solve:

1/3 + 4/5

3/4 - 6/7

4/5 * 3/5

5/6 / 4/5

Honestly, I'm sort of lost on this. I didn't think it would be too difficult, and maybe it isn't but I'm not seeing it :(
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Nov 7 2014 07:01pm
Quote (DD_Rocking @ Nov 7 2014 07:56pm)
So, I'm working on a project right now, and I'm confused on how you would convert a decimal value (say .333333) to a fraction(1/3)

I've looked it up for a while on Google and haven't really been able to get a straight forward answer that I really understand...

Is there a simple way of doing this?


Example for the different situations I would need to solve:

1/3 + 4/5

3/4 - 6/7

4/5 * 3/5

5/6 / 4/5

Honestly, I'm sort of lost on this. I didn't think it would be too difficult, and maybe it isn't but I'm not seeing it :(


you asked about converting decimals to fractions, but the problem you need to solve are just fraction arithmetic. can you explain what converting decimals to fractions has to do with fraction arithmetic?

/edit: risking stating the obvious:
to add/subtract fractions, create a common denominator and add/subtract the numerator.
to multiply fractions, multiply the numerator and denominator.
to divide fractions, flip the numerator/denominator of the second and multiply.
reduce at the end.

This post was edited by carteblanche on Nov 7 2014 07:04pm
Member
Posts: 13,425
Joined: Sep 29 2007
Gold: 0.00
Warn: 20%
Nov 7 2014 07:02pm
You just multiply by which fraction you want.

0.333333333 * 3 is .999999999 (1/3)

0.333333333 * 5 is 1.6666666 (2/5)

This is a very loose way to convert fractions.

As for fraction math just use bedmas in the interpreter/compiler:

Code
irb(main):003:0> hmm = (1.0/3.0) + (4.0/5.0)
=> 1.1333333333333333
irb(main):005:0> puts "#{hmm * 15}/15"
17.0/15
=> nil


so the answer would be 17/15 or 1 and 2/15

Now of course most answers will not be clean like this.

This post was edited by AbDuCt on Nov 7 2014 07:03pm
Member
Posts: 1,995
Joined: Jun 28 2006
Gold: 7.41
Nov 7 2014 07:25pm
Are you reading the expression from console?

In that case are you reading in num1, den1, num2, den2, doing num1/den1 to get decimal value dec1, then num2/den2 to get dec2, then doing dec1 + dec2 = sum, and Now you want to convert sum to a fraction?

If this is what you are doing, then stop it.

You have the parts of the fraction read in. You don't need to convert to a decimal.

Multiply num1 with den2 to get num3. Multiply num2 with den1 to get num4. Add these together to get num5.
Multiply den1 and den2 together to get den3.

Find the gcd of num5 and den3.
Divide num5 by gcd to get resultNumerator
Divide den3 by gcd to get resultDenominator

You have successfully added two fractions together.

And yes, I used confusing variable names for maximum confusion.
Member
Posts: 13,042
Joined: Apr 12 2008
Gold: 0.00
Nov 7 2014 07:27pm
Quote (Minkomonster @ Nov 7 2014 08:25pm)
Are you reading the expression from console?

In that case are you reading in num1, den1, num2, den2, doing num1/den1 to get decimal value dec1, then num2/den2 to get dec2, then doing dec1 + dec2 = sum, and Now you want to convert sum to a fraction?

If this is what you are doing, then stop it.

You have the parts of the fraction read in. You don't need to convert to a decimal.

Multiply num1 with den2 to get num3. Multiply num2 with den1 to get num4. Add these together to get num5.
Multiply den1 and den2 together to get den3.

Find the gcd of num5 and den3.
Divide num5 by gcd to get resultNumerator
Divide den3 by gcd to get resultDenominator

You have successfully added two fractions together.

And yes, I used confusing variable names for maximum confusion.


This is basically what I need lol

But, how would I find the GCD of a dynamic input? (could be any fraction inputted by the user)
Lol, I haven't had to do this type of math in Java before, so it's really new to me; finding GCD and all of a dynamically inputted fraction.

This post was edited by DD_Rocking on Nov 7 2014 07:28pm
Member
Posts: 1,995
Joined: Jun 28 2006
Gold: 7.41
Nov 7 2014 07:32pm
Quote (DD_Rocking @ Nov 7 2014 08:27pm)
This is basically what I need lol

But, how would I find the GCD of a dynamic input? (could be any fraction inputted by the user)
Lol, I haven't had to do this type of math in Java before, so it's really new to me; finding GCD and all of a dynamically inputted fraction.


The same way you would find the gcd of any 2 numbers. With euclid's algorithm:

Code
public int gcd(int a, int b) { return b==0 ? a : gcd(b, a%b); }
Member
Posts: 13,042
Joined: Apr 12 2008
Gold: 0.00
Nov 7 2014 08:22pm
Quote (Minkomonster @ Nov 7 2014 08:32pm)
The same way you would find the gcd of any 2 numbers. With euclid's algorithm:

Code
public int gcd(int a, int b) { return b==0 ? a : gcd(b, a%b); }


So, this is basically as long as b != 0, it will continuously call the same method with the parameters of b, and a modulus b until b is 0?

I'm looking at some examples and looks like that but I'm not sure...
Member
Posts: 1,995
Joined: Jun 28 2006
Gold: 7.41
Nov 7 2014 08:29pm
Quote (DD_Rocking @ Nov 7 2014 09:22pm)
So, this is basically as long as b != 0, it will continuously call the same method with the parameters of b, and a modulus b until b is 0?

I'm looking at some examples and looks like that but I'm not sure...


Let's reduce 12/40

a = 12, b = 40

gcd(12,40) = gcd(40,12%40=12) = gcd(12, 40%12=4) = gcd(4, 12%4=0) = 4

12/4 = 3

40/4 = 10

3/10

This post was edited by Minkomonster on Nov 7 2014 08:30pm
Member
Posts: 13,042
Joined: Apr 12 2008
Gold: 0.00
Nov 7 2014 08:44pm
Quote (Minkomonster @ Nov 7 2014 09:29pm)
Let's reduce 12/40

a = 12, b = 40

gcd(12,40) = gcd(40,12%40=12) = gcd(12, 40%12=4) = gcd(4, 12%4=0) = 4

12/4 = 3

40/4 = 10

3/10


So would this gcd method sort of look like:

Code

public int gcd(int numerator, int denominator) {
if(denominator == 0) {
return numerator;
}
return gcd(denominator, numerator%denominator);
}


So this method basically keeps flipping the parameters and using modulus to eventually get the denominator to 0, and once that occurs, it returns the numerator value?
Member
Posts: 1,995
Joined: Jun 28 2006
Gold: 7.41
Nov 7 2014 08:59pm
Quote (DD_Rocking @ Nov 7 2014 09:44pm)
So would this gcd method sort of look like:

Code
public int gcd(int numerator, int denominator) {
    if(denominator == 0) {
        return numerator;
    }
    return gcd(denominator, numerator%denominator);
}


So this method basically keeps flipping the parameters and using modulus to eventually get the denominator to 0, and once that occurs, it returns the numerator value?


Your method is equivalent to mine. You just expanded the ternary operator and gave it meaningful variable names.
Go Back To Programming & Development Topic List
123Next
Add Reply New Topic New Poll