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

sunlan pushed a commit to branch GROOVY_3_0_X
in repository https://gitbox.apache.org/repos/asf/groovy.git


The following commit(s) were added to refs/heads/GROOVY_3_0_X by this push:
     new 996e87e  Avoid toString() for converting integer types to BigInteger 
and BigDecimal
996e87e is described below

commit 996e87ede0d9ba94b2a93b046b33a42c5268a776
Author: MoonFruit <[email protected]>
AuthorDate: Wed Dec 2 23:27:13 2020 +0800

    Avoid toString() for converting integer types to BigInteger and BigDecimal
---
 .../groovy/runtime/typehandling/NumberMath.java    | 22 ++++++++++++++++++++--
 .../runtime/typehandling/NumberMathTest.groovy     | 14 ++++++++++++++
 2 files changed, 34 insertions(+), 2 deletions(-)

diff --git 
a/src/main/java/org/codehaus/groovy/runtime/typehandling/NumberMath.java 
b/src/main/java/org/codehaus/groovy/runtime/typehandling/NumberMath.java
index 37fa65f..299c524 100644
--- a/src/main/java/org/codehaus/groovy/runtime/typehandling/NumberMath.java
+++ b/src/main/java/org/codehaus/groovy/runtime/typehandling/NumberMath.java
@@ -168,11 +168,29 @@ public abstract class NumberMath {
     }
 
     public static BigDecimal toBigDecimal(Number n) {
-        return (n instanceof BigDecimal ? (BigDecimal) n : new 
BigDecimal(n.toString()));
+        if (n instanceof BigDecimal) {
+            return (BigDecimal) n;
+        }
+        if (n instanceof BigInteger) {
+            return new BigDecimal((BigInteger) n);
+        }
+        if (n instanceof Integer || n instanceof Long || n instanceof Byte || 
n instanceof Short) {
+            return BigDecimal.valueOf(n.longValue());
+        }
+        return new BigDecimal(n.toString());
     }
 
     public static BigInteger toBigInteger(Number n) {
-        return (n instanceof BigInteger ? (BigInteger) n : new 
BigInteger(n.toString()));
+        if (n instanceof BigInteger) {
+            return (BigInteger) n;
+        }
+        if (n instanceof Integer || n instanceof Long || n instanceof Byte || 
n instanceof Short) {
+            return BigInteger.valueOf(n.longValue());
+        }
+        if (n instanceof BigDecimal) {
+            return ((BigDecimal) n).toBigIntegerExact();
+        }
+        return new BigInteger(n.toString());
     }
 
     /**
diff --git 
a/src/test/org/codehaus/groovy/runtime/typehandling/NumberMathTest.groovy 
b/src/test/org/codehaus/groovy/runtime/typehandling/NumberMathTest.groovy
index 38aed18..dbf0d6a 100644
--- a/src/test/org/codehaus/groovy/runtime/typehandling/NumberMathTest.groovy
+++ b/src/test/org/codehaus/groovy/runtime/typehandling/NumberMathTest.groovy
@@ -109,6 +109,7 @@ class NumberMathTest extends GroovyTestCase {
         def BD1 = new BigDecimal("1.0")
         def BD2 = new BigDecimal("2.0")
         def BD20 = new BigDecimal("2.00")
+        def BD100 = new BigDecimal(new BigInteger(1), -2) // 100
 
         assert I1 / I2 instanceof BigDecimal
         assert I1 / I2 == new BigDecimal("0.5")
@@ -139,6 +140,19 @@ class NumberMathTest extends GroovyTestCase {
         assert I2 / I3 == new BigDecimal("0.6666666667")
 
         assert I1 / BD2 instanceof BigDecimal
+        assert I1 / BD2 == new BigDecimal("0.5")
+
+        assert I1 / BD20 instanceof BigDecimal
+        assert I1 / BD20 == new BigDecimal("0.5")
+
+        assert BI1 / BD2 instanceof BigDecimal
+        assert BI1 / BD2 == new BigDecimal("0.5")
+
+        assert I1 / BD100 instanceof BigDecimal
+        assert I1 / BD100 == new BigDecimal("0.01")
+
+        assert BI1 / BD100 instanceof BigDecimal
+        assert BI1 / BD100 == new BigDecimal("0.01")
 
         //Test keeping max scale of (L, R or 10)
         def BBD1 = new BigDecimal("0.12345678901234567")

Reply via email to