Format Double as Fraction [closed]

2020-06-17 05:27发布

Is there a library that will convert a Double to a String with the whole number, followed by a fraction?

For example

1.125 = 1 1/8

I am only looking for fractions to a 64th of an inch.

10条回答
ゆ 、 Hurt°
2楼-- · 2020-06-17 05:56

How about org.apache.commons.math ? They have a Fraction class that takes a double.

http://commons.apache.org/math/api-1.2/org/apache/commons/math/fraction/Fraction.html

You should be able to extend it and give it functionality for the 64th. And you can also add a toString that will easily print out the whole number part of the fraction for you.

Fraction(double value, int maxDenominator) Create a fraction given the double value and maximum denominator.

查看更多
孤傲高冷的网名
3楼-- · 2020-06-17 06:00

My code looks like this.

public static int gcd(int a, int b)
    {
        if (b == 0)
            return a;
        else
            return gcd(b, a % b);
    }

public static String doubleToStringFraction(Double d)
    {
        StringBuffer result = new StringBuffer(" " + ((int) Math.floor(d)));
        int whole = (int) ((d - Math.floor(d)) * 10000);
        int gcd = gcd(whole, 10000);
        result.append(" " + (whole / gcd) + "/" + 10000 / gcd + " ");
        return result.toString();
    }
查看更多
趁早两清
4楼-- · 2020-06-17 06:02

i wrote this for my project i hope it could be usefull:

//How to "Convert" double to fraction("a/b") - kevinlopez@unitec.edu
private boolean isInt(double number){
    if(number%2==0 ||(number+1)%2==0){
        return true;
    }
    return false;
}
private String doubleToFraction(double doub){
    //we get the whole part
    int whole = (int)doub;
    //we get the rest
    double rest = doub - (double)whole;
    int numerator=1,denominator=1;
    //if the whole part of the number is greater than 0
    //we'll try to transform the rest of the number to an Integer
    //by multiplying the number until it become an integer
    if(whole >=1){
        for(int i = 2; ; i++){
            /*when we find the "Integer" number(it'll be the numerator)
             * we also found the denominator(i,which is the number that transforms the number to integer)
             * For example if we have the number = 2.5 when it is multiplied by 2
             * now it's 5 and it's integer, now we have the numerator(the number (2.5)*i(2) = 5)
             * and the denominator i = 2
             */
            if(isInt(rest*(double)i)){
                numerator = (int)(rest*(double)i);
                denominator = i;
                break;
            }
            if(i>10000){
                //if i is greater than 10000 it's posible that the number is irrational
                //and it can't be represented as a fractional number
                return doub+"";
            }
        }
        //if we have the number 3.5 the whole part is 3 then we have the rest represented in fraction 0.5 = 1/2
        //so we have a mixed fraction 3+1/2 = 7/2
        numerator = (whole*denominator)+numerator;
    }else{
        //If not we'll try to transform the original number to an integer
        //with the same process
        for(int i = 2; ; i++){
            if(isInt(doub*(double)i)){
                numerator = (int)(doub*(double)i);
                denominator = i;
                break;
            }
            if(i>10000){
                return doub+"";
            }
        }
    }
    return numerator+"/"+denominator;
}
查看更多
在下西门庆
5楼-- · 2020-06-17 06:05

Function for this in a C-variant called LPC follows. Some notes:

  1. Addition to input value at beginning is to try to cope with precision issues that otherwise love to wind up telling you that 5 is 4 999999/1000000.
  2. The to_int() function truncates to integer.
  3. Language has a to_string() that will turn some floats into exponential notation.

string strfrac(float frac) {
    int main = to_int(frac + frac / 1000000.0);
    string out = to_string(main);
    float rem = frac - to_float(main);
    string rep;
    if(rem > 0 && (to_int(rep = to_string(rem)) || member(rep, 'e') == Null)) {
        int array primes = ({ 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47 });
        string base;
        int exp;
        int num;
        int div;
        if(sscanf(rep, "%se%d", base, exp) == 2) {
            num = to_int(replace(base, ".", ""));
            div = to_int(pow(10, abs(exp)));
        } else {
            rep = rep[2..];
            num = to_int(rep);
            div = to_int(pow(10, strlen(rep)));
        }
        foreach(int prime : primes) {
            if(prime > num)
                break;
            while((num / prime) * prime == num && (div / prime) * prime == div) {
                num /= prime;
                div /= prime;
            }
        }
        out += " " + num + "/" + div;
    }
    return out;
}
查看更多
登录 后发表回答