You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@groovy.apache.org by GitBox <gi...@apache.org> on 2020/11/30 13:34:19 UTC

[GitHub] [groovy] danielsun1106 commented on a change in pull request #1437: Avoid toString() for converting integer types to BigInteger and BigDecimal

danielsun1106 commented on a change in pull request #1437:
URL: https://github.com/apache/groovy/pull/1437#discussion_r532598636



##########
File path: src/main/java/org/codehaus/groovy/runtime/typehandling/NumberMath.java
##########
@@ -168,11 +168,26 @@ public static boolean isBigInteger(Number number) {
     }
 
     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 Byte || n instanceof Short || n instanceof Integer || n instanceof Long) {

Review comment:
       I suggest to put Integer and Long at the beginning to avoid checks as possible as we could, because we use Integer and Long more often in the life 😉

##########
File path: src/main/java/org/codehaus/groovy/runtime/typehandling/NumberMath.java
##########
@@ -168,11 +168,26 @@ public static boolean isBigInteger(Number number) {
     }
 
     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 Byte || n instanceof Short || n instanceof Integer || n instanceof Long) {
+            return new BigDecimal(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 Byte || n instanceof Short || n instanceof Integer || n instanceof Long) {

Review comment:
       Ditto




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org