programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Jeanne Boyarsky
• Ron McLeod
• Paul Clapham
• Liutauras Vilda
Sheriffs:
• paul wheaton
• Rob Spoor
• Devaka Cooray
Saloon Keepers:
• Stephan van Hulst
• Tim Holloway
• Carey Brown
• Frits Walraven
• Tim Moores
Bartenders:
• Mikalai Zaikin

# Converting a double value to a fraction

Ranch Hand
Posts: 455
• Number of slices to send:
Optional 'thank-you' note:
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!

Greenhorn
Posts: 18
• Number of slices to send:
Optional 'thank-you' note:
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
Posts: 455
• Number of slices to send:
Optional 'thank-you' note:
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
Posts: 18
• Number of slices to send:
Optional 'thank-you' note:
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
Posts: 455
• Number of slices to send:
Optional 'thank-you' note:
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?

Bartender
Posts: 1844
• Number of slices to send:
Optional 'thank-you' note:
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.

Ranch Hand
Posts: 83
• Number of slices to send:
Optional 'thank-you' note:
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.)

 Don't get me started about those stupid light bulbs.