• Post Reply Bookmark Topic Watch Topic
  • New Topic
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
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Converting a double value to a fraction

 
Ranch Hand
Posts: 455
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Eclipse IDE Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.)
     
    PI day is 3.14 (march 14th) and is also einstein's birthday. And this is merely a tiny ad:
    Smokeless wood heat with a rocket mass heater
    https://woodheat.net
    reply
      Bookmark Topic Watch Topic
    • New Topic