Jennifer Sohl

Ranch Hand

Posts: 455

posted 14 years ago

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!

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!

Edwin Keeton

Greenhorn

Posts: 18

posted 14 years ago

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;

}

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

Edwin Keeton

Greenhorn

Posts: 18

Jennifer Sohl

Ranch Hand

Posts: 455

posted 14 years ago

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?

Can someone please explain to me what is happening?

Roy Tock

Ranch Hand

Posts: 83

posted 14 years ago

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.)

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.)