how do i convert decimal to fraction in Java?

Thank you.
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Algorithm :-

Suppose you have 1.5, the decimal part if 0.5
-> Multiply the numerator and denominator by 10, you get 10/15.
-> Find the greated common divisor - 5.
-> Divide both numerator and denominator by 5, you get 2/3.

Mick BarryJava DeveloperCommented:
commons lang has a method for it


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
I have no idea about the function in Commons Lang.
This is what I thought

        iint numerator = -1;
        int denomianator = -1;
        double d = 1.5;
        // Find num of decimal digits
        Double temp = new Double(d);
        String s = temp.toString(d);
        int numOfDecimals = s.length() - s.indexOf(".")-1;
        denomianator = (int) Math.pow(10, numOfDecimals);
        numerator = (int) (temp.intValue() * Math.pow(10, numOfDecimals) + (temp.floatValue()-temp.intValue())* Math.pow(10, numOfDecimals));
        System.out.println(numerator+"/"+ denomianator);

This is a test code that changes the deicmal to fraction. All you need to do is reduce the fraction further
Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

jiwonmanAuthor Commented:
a b .

do you think I can implement that to my previous problem? I wish I can try it but not enough time for me as of right now.

your method makes sense btw.
shouldn't the fraction be written as 1 and 1/2
here is what you need to do -

create a new function called void printFraction(double num)
move all convesion code within this function
when you do a sysout for the array, call this function Sysout(printFraction(array[i][j]));

I am sure you can do this, let  me know if you need any help.
You donot need to change the existing code/login, all you need to do is add code.
There is a method in org.apache.commons.lang.math.Fraction class.

This method has the prototype:

static Fraction getFraction(double value)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.