This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.

I am working on an application where users will have the choice of displaying Widths,lengths and heights in decimal or fraction format. What do I need to do to convert a double value into a String representaion of a fraction for that value? Thanks for any help!

First split the double into two parts, the integer part and the fractional part. Then take the fractional part of the number and create a third number which is a power of 10 where the exponent is the length of the fractional part of the original number. The fractional part of the number is is the numerator and the power of ten number is the denominator. Now divide numerator and denominator by their greatest common divisor. Then display all three numbers by concatenating them in sequence with the appropriate separator symbols. For example, 2.25 = Integer part is 2; fractional part is 25/100 GCD is 25, so 1/4 is the fractional representation In code it might look something like this: String convertToFraction(double d) { // get the whole number part long i = (long) Math.ceil(d); // get the fractional part double numerator = d - i; // Convert the fractional part to a String String frac = new Double(numerator).toString(); // We only want what's to the right of the //decimal point frac = frac.substring(frac.indexOf('.')); // Put the String back into a double numerator = Double.parseDouble(frac); int power = frac.length(); double denominator = Math.pow(10, power); // implement findGCD() int gcd = findGCD(numerator, denominator); numerator /= gcd; denominator /= gcd; return i + "-" + (long) numerator + "/" + (long)denominator; }

Jennifer Sohl
Ranch Hand

Joined: Feb 28, 2001
Posts: 455

posted

0

Thanks for the reply. In your example code, you have a method called findGCD() that takes two double parameters. What does this method do?

Edwin Keeton
Greenhorn

Joined: Jul 22, 2002
Posts: 18

posted

0

It finds the greatest common divisor of the two numbers. You can find an implementation in any algorithm book, or elsewhere on the web.

Jennifer Sohl
Ranch Hand

Joined: Feb 28, 2001
Posts: 455

posted

0

Hello again. I've found some code that converts decimal values to fractions wonderfully. However, whenever I try to convert .062, I don't get the expected result. I want it to show 1/16 instead, I get 31/500. Does the same thing with with basically any decimal values that begin with '0' right after the decimal point. Can someone please explain to me what is happening?

At least from this example, the software is correct. .062 does not equal 1/16; .0625 does. .062 equals 62/1000, which reduces to 31/500, the answer given by the software.

Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.

Jennifer, it sounds like you want to use powers of two in your denominator, rather than powers of 10. You can do that. Here's how:

Choose the largest power of two you want for your denominator. Let's say it's 64, for the sake of discussion; any will do.

First, separate the integer part of the decimal from the fractional. It sounds like you've already done that.

Next, multiply the fractional part of the decimal by 64, and round to the nearest integer (using Math.round()). That'll give you the numerator of the fraction.

If you want to, reduce the fraction using the code you've already written.

This method won't give you an exact representation, as Joel pointed out, but it'll be close enough for your need, because you chose the denominator. (If you want less error, choose a bigger denominator.)