You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by er...@apache.org on 2019/05/22 19:23:10 UTC

[commons-numbers] 01/04: NUMBERS-97: replacing pow method

This is an automated email from the ASF dual-hosted git repository.

ericbarnhill pushed a commit to branch fraction-dev
in repository https://gitbox.apache.org/repos/asf/commons-numbers.git

commit 092e816b4e0bf369ff08c7d8b4e640b15ebeb47d
Author: Eric Barnhill <eb...@irhythmtech.com>
AuthorDate: Fri May 17 15:49:56 2019 -0700

    NUMBERS-97: replacing pow method
---
 .../commons/numbers/fraction/BigFraction.java      | 27 ++++++++++++++++++++++
 1 file changed, 27 insertions(+)

diff --git a/commons-numbers-fraction/src/main/java/org/apache/commons/numbers/fraction/BigFraction.java b/commons-numbers-fraction/src/main/java/org/apache/commons/numbers/fraction/BigFraction.java
index 689228a..283475c 100644
--- a/commons-numbers-fraction/src/main/java/org/apache/commons/numbers/fraction/BigFraction.java
+++ b/commons-numbers-fraction/src/main/java/org/apache/commons/numbers/fraction/BigFraction.java
@@ -20,6 +20,8 @@ import java.io.Serializable;
 import java.math.BigDecimal;
 import java.math.BigInteger;
 import java.math.RoundingMode;
+import java.text.NumberFormat;
+
 import org.apache.commons.numbers.core.ArithmeticUtils;
 
 /**
@@ -1173,6 +1175,31 @@ public class BigFraction
         }
         return str;
     }
+    
+    
+    /**
+     * Parses a string that would be produced by {@link #toString()}
+     * and instantiates the corresponding object.
+     *
+     * @param s String representation.
+     * @return an instance.
+     * @throws FractionException if the string does not
+     * conform to the specification.
+     */
+    public static BigFraction parse(String s) {
+        s = s.replace(",", "");
+        final int slashLoc = s.indexOf("/");
+        // if no slash, parse as single number
+        if (slashLoc == -1) {
+            return BigFraction.of(new BigInteger(s.trim()));
+        } else {
+            final BigInteger num = new BigInteger(
+                    s.substring(0, slashLoc).trim());
+            final BigInteger denom = new BigInteger(s.substring(slashLoc + 1).trim());
+            return of(num, denom);
+        }
+    }
+
 
     /**
      * Check that the argument is not null and throw a NullPointerException