Converting decimal to fraction

Dando18

I have a method that is converting a decimal (double value) into a fraction and putting the numerator and denominator values into an int[] of size 2.

Testing it works out fine for most values except when I hit 0.0001. Then the return value is 1.0/1.0.

The method:

private static int[] toFractionPos(double x){
    String[] parts = Double.toString(x).split("\\.");
    double den = Math.pow(10, parts[1].length()); //denominator
    double num = Double.parseDouble(parts[0]) * den + Double.parseDouble(parts[1]); //numerator
    return reduceFraction((int)num, (int)den);
}

reduceFraction() method:

public static int[] reduceFraction(int num, int den){
    int gcf = GCF(num, den); //greatest common factor
    int[] rf = {num/gcf, den/gcf};
    return rf;
}

Thanks!

fps

The algorithm seems fine. However, using double is not suitable for this kind of problem, because precision decreases as the scale grows.

You should use BigDecimal and BigInteger instead. I've roughly modified your example so that it works with them, but I haven't taken care of details, i.e. parsing the String shouldn't be necessary since scale can be retrieved from a BigDecimal with a getter, you can configure different rounding modes, etc:

import java.math.BigDecimal;
import java.math.BigInteger;

public class Sample {

    static int[] toFractionPos(BigDecimal x) {
        String[] parts = x.toString().split("\\.");
        BigDecimal den = BigDecimal.TEN.pow(parts[1].length()); // denominator
        BigDecimal num = (new BigDecimal(parts[0]).multiply(den)).add(new BigDecimal(parts[1])); // numerator
        return reduceFraction(num.intValue(), den.intValue());
    }

    static int[] reduceFraction(int num, int den) {
        int gcd = BigInteger.valueOf(num).gcd(BigInteger.valueOf(den)).intValue(); // greatest
                                                                                   // common
                                                                                   // divisor
        int[] rf = { num / gcd, den / gcd };
        return rf;
    }

    public static void main(String[] args) {
        int[] fraction = toFractionPos(new BigDecimal("0.0001"));
        System.out.println(fraction[0] + "/" + fraction[1]); // 1/10000
    }
}

Note: optimizations left as an excercise ;)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Converting a Decimal to a Fraction

From Dev

Converting decimal to fraction

From Dev

Exact fraction arithmetic without Matlab converting to decimal representation?

From Dev

converting binary/decimal fraction to hexadecimal in java -unexpected output

From Dev

Decimal to Fraction

From Dev

Decimal to Fraction

From Dev

Decimal to Fraction conversion in Swift

From Dev

Convert fraction to decimal in NASM

From Dev

Convert fraction to decimal in NASM

From Dev

Recurring fraction to decimal on BigDecimal

From Java

Algorithm for converting fraction to binary

From Dev

convert decimal number to fraction in javascript or closest fraction

From Dev

convert decimal number to fraction in javascript or closest fraction

From Dev

Converting to decimal

From Java

Convert a fraction to a decimal in Google Sheets

From Dev

How to convert binary fraction to decimal

From Java

How to convert a decimal number into fraction?

From Dev

How to convert Octal Fraction to Decimal?

From Dev

How to turn decimal into a fraction - Swift

From Dev

Choose if your number is a fraction or a decimal

From Dev

Converting time to fraction of days in python

From Dev

The number 0.6 is not converting to a fraction correctly

From Dev

How to convert a binary fraction number into decimal fraction number?

From Dev

How to convert a binary fraction number into decimal fraction number?

From Dev

Stuck converting decimal with decimal values

From Dev

How to cast the fraction into 2 decimal point?

From Dev

How can I convert fraction to double or decimal?

From Dev

Convert fraction to string with repeating decimal places in brackets

From Dev

Decimal fraction conversion to binary - how to convert 0.1?

Related Related

HotTag

Archive