commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From l..@apache.org
Subject svn commit: r762095 - in /commons/proper/math/trunk/src: java/org/apache/commons/math/fraction/Fraction.java site/xdoc/changes.xml test/org/apache/commons/math/fraction/FractionTest.java
Date Sun, 05 Apr 2009 14:29:03 GMT
Author: luc
Date: Sun Apr  5 14:29:03 2009
New Revision: 762095

URL: http://svn.apache.org/viewvc?rev=762095&view=rev
Log:
added Fraction.toString()
Jira: MATH-256

Modified:
    commons/proper/math/trunk/src/java/org/apache/commons/math/fraction/Fraction.java
    commons/proper/math/trunk/src/site/xdoc/changes.xml
    commons/proper/math/trunk/src/test/org/apache/commons/math/fraction/FractionTest.java

Modified: commons/proper/math/trunk/src/java/org/apache/commons/math/fraction/Fraction.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/java/org/apache/commons/math/fraction/Fraction.java?rev=762095&r1=762094&r2=762095&view=diff
==============================================================================
--- commons/proper/math/trunk/src/java/org/apache/commons/math/fraction/Fraction.java (original)
+++ commons/proper/math/trunk/src/java/org/apache/commons/math/fraction/Fraction.java Sun
Apr  5 14:29:03 2009
@@ -627,4 +627,26 @@
         return new Fraction(numerator, denominator);
     }
 
+    /**
+     * <p>
+     * Returns the <code>String</code> representing this fraction, ie
+     * "num / dem" or just "num" if the denominator is one.
+     * </p>
+     * 
+     * @return a string representation of the fraction.
+     * @see java.lang.Object#toString()
+     */
+    @Override
+    public String toString() {
+        String str = null;
+        if (denominator == 1) {
+            str = Integer.toString(numerator);
+        } else if (numerator == 0) {
+            str = "0";
+        } else {
+            str = numerator + " / " + denominator;
+        }
+        return str;
+    }
+
 }

Modified: commons/proper/math/trunk/src/site/xdoc/changes.xml
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/site/xdoc/changes.xml?rev=762095&r1=762094&r2=762095&view=diff
==============================================================================
--- commons/proper/math/trunk/src/site/xdoc/changes.xml (original)
+++ commons/proper/math/trunk/src/site/xdoc/changes.xml Sun Apr  5 14:29:03 2009
@@ -39,6 +39,9 @@
   </properties>
   <body>
     <release version="2.0" date="TBD" description="TBD">
+      <action dev="luc" type="add" issue="MATH-256">
+        Added  Fraction.toString()
+      </action>
       <action dev="luc" type="add">
         Added  add/subtract/multiply/divide functions with integer parameters to Fraction
       </action>

Modified: commons/proper/math/trunk/src/test/org/apache/commons/math/fraction/FractionTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/org/apache/commons/math/fraction/FractionTest.java?rev=762095&r1=762094&r2=762095&view=diff
==============================================================================
--- commons/proper/math/trunk/src/test/org/apache/commons/math/fraction/FractionTest.java
(original)
+++ commons/proper/math/trunk/src/test/org/apache/commons/math/fraction/FractionTest.java
Sun Apr  5 14:29:03 2009
@@ -564,4 +564,10 @@
         assertEquals(Fraction.getReducedFraction
                 (1, -1).getNumerator(), -1);
     }
+
+    public void testToString() {
+        assertEquals("0", new Fraction(0, 3).toString());
+        assertEquals("3", new Fraction(6, 2).toString());
+        assertEquals("2 / 3", new Fraction(18, 27).toString());
+    }
 }



Mime
View raw message