This is an automated email from the ASF dual-hosted git repository.
garydgregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-beanutils.git
The following commit(s) were added to refs/heads/master by this push:
new b54cdf33 preserve magnitude in BigInteger and BigDecimal locale
converters (#401)
b54cdf33 is described below
commit b54cdf3325622b1ad94c980e564bef6d1c4da554
Author: Dexter.k <[email protected]>
AuthorDate: Sun Jun 21 21:19:45 2026 +0000
preserve magnitude in BigInteger and BigDecimal locale converters (#401)
---
.../locale/converters/BigDecimalLocaleConverter.java | 5 +++++
.../locale/converters/BigIntegerLocaleConverter.java | 10 ++++++++++
.../locale/converters/DecimalLocaleConverter.java | 12 ++++++++++++
.../converters/BigDecimalLocaleConverterTest.java | 11 +++++++++++
.../converters/BigIntegerLocaleConverterTest.java | 14 ++++++++++++++
5 files changed, 52 insertions(+)
diff --git
a/src/main/java/org/apache/commons/beanutils2/locale/converters/BigDecimalLocaleConverter.java
b/src/main/java/org/apache/commons/beanutils2/locale/converters/BigDecimalLocaleConverter.java
index 89d1d90d..a54ece4d 100644
---
a/src/main/java/org/apache/commons/beanutils2/locale/converters/BigDecimalLocaleConverter.java
+++
b/src/main/java/org/apache/commons/beanutils2/locale/converters/BigDecimalLocaleConverter.java
@@ -88,4 +88,9 @@ public class BigDecimalLocaleConverter extends
DecimalLocaleConverter<BigDecimal
}
}
+ @Override
+ protected boolean isParseBigDecimal() {
+ return true;
+ }
+
}
diff --git
a/src/main/java/org/apache/commons/beanutils2/locale/converters/BigIntegerLocaleConverter.java
b/src/main/java/org/apache/commons/beanutils2/locale/converters/BigIntegerLocaleConverter.java
index 849531df..ded930f7 100644
---
a/src/main/java/org/apache/commons/beanutils2/locale/converters/BigIntegerLocaleConverter.java
+++
b/src/main/java/org/apache/commons/beanutils2/locale/converters/BigIntegerLocaleConverter.java
@@ -17,6 +17,7 @@
package org.apache.commons.beanutils2.locale.converters;
+import java.math.BigDecimal;
import java.math.BigInteger;
import java.text.ParseException;
import java.util.Locale;
@@ -81,7 +82,16 @@ public class BigIntegerLocaleConverter extends
DecimalLocaleConverter<BigInteger
return (BigInteger) result;
}
+ if (result instanceof BigDecimal) {
+ return ((BigDecimal) result).toBigInteger();
+ }
+
return BigInteger.valueOf(result.longValue());
}
+ @Override
+ protected boolean isParseBigDecimal() {
+ return true;
+ }
+
}
diff --git
a/src/main/java/org/apache/commons/beanutils2/locale/converters/DecimalLocaleConverter.java
b/src/main/java/org/apache/commons/beanutils2/locale/converters/DecimalLocaleConverter.java
index bf65ff51..31834c0c 100644
---
a/src/main/java/org/apache/commons/beanutils2/locale/converters/DecimalLocaleConverter.java
+++
b/src/main/java/org/apache/commons/beanutils2/locale/converters/DecimalLocaleConverter.java
@@ -97,6 +97,17 @@ public class DecimalLocaleConverter<T extends Number>
extends BaseLocaleConverte
super(defaultValue, locale, pattern, useDefault, locPattern);
}
+ /**
+ * Tests whether the underlying {@link DecimalFormat} should parse into a
{@link java.math.BigDecimal} so that magnitude and precision are preserved.
+ * Subclasses that build {@link java.math.BigInteger} or {@link
java.math.BigDecimal} values override this to return {@code true}; the narrowing
+ * converters keep the default {@code Long} / {@code Double} result.
+ *
+ * @return {@code true} to parse into a {@link java.math.BigDecimal},
{@code false} otherwise.
+ */
+ protected boolean isParseBigDecimal() {
+ return false;
+ }
+
/**
* Converts the specified locale-sensitive input object into an output
object of the specified type.
*
@@ -117,6 +128,7 @@ public class DecimalLocaleConverter<T extends Number>
extends BaseLocaleConverte
// representation, each call to getInstance actually returns a new
// object.
final DecimalFormat formatter = (DecimalFormat)
NumberFormat.getInstance(locale);
+ formatter.setParseBigDecimal(isParseBigDecimal());
// if some constructors default pattern to null, it makes only sense
// to handle null pattern gracefully
diff --git
a/src/test/java/org/apache/commons/beanutils2/converters/BigDecimalLocaleConverterTest.java
b/src/test/java/org/apache/commons/beanutils2/converters/BigDecimalLocaleConverterTest.java
index ca23ce0a..922e4f7a 100644
---
a/src/test/java/org/apache/commons/beanutils2/converters/BigDecimalLocaleConverterTest.java
+++
b/src/test/java/org/apache/commons/beanutils2/converters/BigDecimalLocaleConverterTest.java
@@ -179,6 +179,17 @@ class BigDecimalLocaleConverterTest extends
AbstractLocaleConverterTest<BigDecim
}
+ /**
+ * A value with more significant digits than a {@code double} can hold
must not be rounded through {@code double}.
+ */
+ @Test
+ void testConvertLargeValueKeepsPrecision() {
+ converter = BigDecimalLocaleConverter.builder().get();
+
+ final String big = "9999999999999999999";
+ convertValueNoPattern(converter, big, new BigDecimal(big));
+ }
+
/**
* Test Converter(defaultValue, locale, pattern, localizedPattern)
constructor
*/
diff --git
a/src/test/java/org/apache/commons/beanutils2/converters/BigIntegerLocaleConverterTest.java
b/src/test/java/org/apache/commons/beanutils2/converters/BigIntegerLocaleConverterTest.java
index 62a1bf27..786d5844 100644
---
a/src/test/java/org/apache/commons/beanutils2/converters/BigIntegerLocaleConverterTest.java
+++
b/src/test/java/org/apache/commons/beanutils2/converters/BigIntegerLocaleConverterTest.java
@@ -179,6 +179,20 @@ class BigIntegerLocaleConverterTest extends
AbstractLocaleConverterTest<BigInteg
}
+ /**
+ * A value with more digits than fit in a {@code long} must keep its full
magnitude instead of saturating to {@link Long#MAX_VALUE}.
+ */
+ @Test
+ void testConvertLargeValueKeepsMagnitude() {
+ converter = BigIntegerLocaleConverter.builder().get();
+
+ final String big = "9999999999999999999";
+ convertValueNoPattern(converter, big, new BigInteger(big));
+
+ final String huge = "123456789012345678901234567890";
+ convertValueNoPattern(converter, huge, new BigInteger(huge));
+ }
+
/**
* Test Converter(defaultValue, locale, pattern, localizedPattern)
constructor
*/