You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by pa...@apache.org on 2024/01/10 01:44:21 UTC
(groovy) branch GROOVY_4_0_X updated: backport from master the fix to handle exotic numbers, e.g. Fractions from Apache Commons
This is an automated email from the ASF dual-hosted git repository.
paulk pushed a commit to branch GROOVY_4_0_X
in repository https://gitbox.apache.org/repos/asf/groovy.git
The following commit(s) were added to refs/heads/GROOVY_4_0_X by this push:
new 05b00a04ed backport from master the fix to handle exotic numbers, e.g. Fractions from Apache Commons
05b00a04ed is described below
commit 05b00a04ed0c656ca2dae767583f46db6a599b1f
Author: Paul King <pa...@asert.com.au>
AuthorDate: Wed Jan 10 11:43:30 2024 +1000
backport from master the fix to handle exotic numbers, e.g. Fractions from Apache Commons
---
.../java/org/codehaus/groovy/runtime/typehandling/NumberMath.java | 6 +++++-
1 file changed, 5 insertions(+), 1 deletion(-)
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 9ee014dcd6..4ab279e52f 100644
--- a/src/main/java/org/codehaus/groovy/runtime/typehandling/NumberMath.java
+++ b/src/main/java/org/codehaus/groovy/runtime/typehandling/NumberMath.java
@@ -177,7 +177,11 @@ public abstract class NumberMath {
if (n instanceof Integer || n instanceof Long || n instanceof Byte || n instanceof Short) {
return BigDecimal.valueOf(n.longValue());
}
- return new BigDecimal(n.toString());
+ try {
+ return new BigDecimal(n.toString());
+ } catch(NumberFormatException nfe) {
+ return BigDecimal.valueOf(n.doubleValue());
+ }
}
public static BigInteger toBigInteger(Number n) {