title: Best way to represent a fraction in Java?
body: <p>I'm trying to work with <a href="http://en.wikipedia.org/wiki/Fraction_%28mathematics%29#Vulgar.2C_proper.2C_and_improper_fractions" rel="noreferrer">fractions</a> in Java. </p>
  
  <p>I want to implement arithmetic functions.  For this, I will first require a way to normalize the functions.  I know I can't add 1/6 and 1/2 until I have a common denominator. I will have to add 1/6 and 3/6.  A naive approach would have me add 2/12 and 6/12 and then reduce.  How can I achieve a common denominator with the least performance penalty?  What algorithm is best for this?</p>
  
  <hr>
  
  <p>Version 8 (thanks to <a href="https://stackoverflow.com/users/21499/hstoerr">hstoerr</a>):</p>
  
  <blockquote>
    <p>Improvements include:</p>
    
    <ul>
    <li>the equals() method is now consistent with the compareTo() method</li>
    </ul>
  </blockquote>
  
  <pre><code>final class Fraction extends Number {
      private int numerator;
      private int denominator;
  
      public Fraction(int numerator, int denominator) {
          if(denominator == 0) {
              throw new IllegalArgumentException("denominator is zero");
          }
          if(denominator &lt; 0) {
              numerator *= -1;
              denominator *= -1;
          }
          this.numerator = numerator;
          this.denominator = denominator;
      }
  
      public Fraction(int numerator) {
          this.numerator = numerator;
          this.denominator = 1;
      }
  
      public int getNumerator() {
          return this.numerator;
      }
  
      public int getDenominator() {
          return this.denominator;
      }
  
      public byte byteValue() {
          return (byte) this.doubleValue();
      }
  
      public double doubleValue() {
          return ((double) numerator)/((double) denominator);
      }
  
      public float floatValue() {
          return (float) this.doubleValue();
      }
  
      public int intValue() {
          return (int) this.doubleValue();
      }
  
      public long longValue() {
          return (long) this.doubleValue();
      }
  
      public short shortValue() {
          return (short) this.doubleValue();
      }
  
      public boolean equals(Fraction frac) {
          return this.compareTo(frac) == 0;
      }
  
      public int compareTo(Fraction frac) {
          long t = this.getNumerator() * frac.getDenominator();
          long f = frac.getNumerator() * this.getDenominator();
          int result = 0;
          if(t&gt;f) {
              result = 1;
          }
          else if(f&gt;t) {
              result = -1;
          }
          return result;
      }
  }
  </code></pre>
  
  <hr>
  
  <p>I have removed all previous versions.  My thanks to:</p>
  
  <ul>
  <li><a href="https://stackoverflow.com/users/40310/dave-ray">Dave Ray</a></li>
  <li><a href="https://stackoverflow.com/users/18393/cletus">cletus</a></li>
  <li><a href="https://stackoverflow.com/users/37213/duffymo">duffymo</a></li>
  <li><a href="https://stackoverflow.com/users/41039/james">James</a></li>
  <li><a href="https://stackoverflow.com/users/17712/milhous">Milhous</a></li>
  <li><a href="https://stackoverflow.com/users/20654/oscar-reyes">Oscar Reyes</a></li>
  <li><a href="https://stackoverflow.com/users/44330/jason-s">Jason S</a></li>
  <li><a href="https://stackoverflow.com/users/54591/francisco-canedo">Francisco Canedo</a></li>
  <li><a href="https://stackoverflow.com/users/1471/outlaw-programmer">Outlaw Programmer</a></li>
  <li><a href="https://stackoverflow.com/users/57120/beska">Beska</a></li>
  </ul>