Decimal to Fraction


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 6 of 6

Thread: Decimal to Fraction

Hybrid View

  1. #1
    Join Date
    Nov 2005
    Posts
    8

    Decimal to Fraction

    I've created a Fraction class that will take a double in the constructor and transform it into a printable fraction. I've got everything working fine except for repeating fractions like 1/3. I know I need to change the code for my Fraction constructor (using doubles), but I'm not sure how.

    Here is my fraction class:

    public class Fraction
    {
    int n,d;

    public Fraction()
    {
    n=0;
    d=0;
    }
    public Fraction (double doubleFraction)
    {
    this.gcd((int)(doubleFraction*100),100);
    }
    public Fraction (int numerator, int denominator)
    {
    this.gcd(numerator,denominator);
    }
    private void gcd (int numerator, int denominator)
    {
    int highest;

    if (denominator>numerator)
    highest=denominator;
    else
    highest=numerator;

    for(int x = highest;x>0;x--)
    {
    if (denominator%x==0 && numerator%x==0)
    {
    this.n=numerator/x;
    this.d=denominator/x;
    break;
    }
    }
    }
    public String getFraction()
    {
    int wholeNumber;

    if (this.n == this.d)
    return("" + 1);
    else if (this.d > this.n)
    return (this.n + "/" + this.d);
    else
    {
    Fraction test = new Fraction(this.n,this.d);
    wholeNumber = (int)(test.n/test.d);

    test.gcd(test.n%test.d,test.d);

    return (wholeNumber + " " + test.n + "/" + test.d);
    }
    }
    }

    Thanks for any help.
    Last edited by evenflow58; 12-15-2005 at 11:57 PM.

  2. #2
    Join Date
    Sep 2005
    Location
    istanbul / Turkey
    Posts
    133
    1-)rational numbers may include infinite-long decimal part.
    ( example 1/3 ..)
    and they should have a repeat pattern.
    ( 22/ 7 = 3.142857142857142857142857... )

    2-) if we know the fraction we can find repeating part exactly.
    but if we have a number with infinite-long decimal part
    ( should be represented using finite-precision )
    we can not say exactly which is the fraction.
    ( each precision level may give another fraction )

    problem :
    in math that numbers have infinite-long decimal part.
    ( but inside computer they are limited ofcourse )
    you work finite-long precision ( 1/100 precision ).
    ( but you work less precision level than java )
    so , dont expect to work in java-level-precision .

    forexample if you work with 1/10000 precision :
    number format will be 0.xyzt
    for 0.xyxy you may say xy repeating part
    but this number may be 0.xyxyzxyxyz as a java double value.

    guide :
    -decide wich level precision you want to work .
    -find the longest repeated pattern in decimal part.
    - use this trick :
    0.xxxxxx ===> x / 9 ( x != 9 )
    0.xyxyxy ===> xy / 99 ( x != 99 )
    0.xyzxyz ===> xyz / 999 ( x != 999 )
    ...
    0.99999 =====> 1

  3. #3
    Join Date
    Nov 2006
    Posts
    2
    Hey, i need help converting decimals to fractions in java. could you post your revised program with the fixed errors? or can someone else revise it casuse i dont get how to fix it

    plz help ,Daminan_Sotd
    Last edited by Daminan_sotd; 11-02-2006 at 10:42 PM.

  4. #4
    Join Date
    Dec 2004
    Location
    San Bernardino County, California
    Posts
    1,468
    Have you thought about having a lookup table for the common "infinitely repeating" decimals? Your logic could determine whether you have gone to 5(?) or 10(?) decimal places and you haven't resolved, then look up the pattern and report the fraction which follows that pattern.

    For the infinitely-long non-resolving fractions, it looks like mr1yh1's suggestion will work well.

  5. #5
    Join Date
    Nov 2006
    Posts
    2
    but how do you you make java recognise diffrent patterns like .333333333 and .16666666 ???

  6. #6
    Join Date
    Dec 2004
    Location
    San Bernardino County, California
    Posts
    1,468
    Read the digits after the decimal as an int and use a switch structure. Take a look at

    http://java.sun.com/docs/books/jls/t...s.html#15.17.3

    for performing modulus on a floating point number
    Last edited by nspils; 11-05-2006 at 02:18 PM.

Similar Threads

  1. fraction class [was:need help plzz]
    By malehda3y in forum C++
    Replies: 13
    Last Post: 12-03-2005, 03:35 PM
  2. Conversion of decimal in Oracle to Access 2000
    By Villy Olsson in forum Database
    Replies: 4
    Last Post: 04-06-2002, 09:01 PM
  3. Replies: 2
    Last Post: 09-26-2001, 07:08 PM
  4. Replies: 0
    Last Post: 02-25-2001, 01:58 PM
  5. Textbox for Decimal Number
    By Dante in forum VB Classic
    Replies: 2
    Last Post: 06-23-2000, 03:45 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center