Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -493,6 +493,9 @@ private <T> T toNumber(final Class<?> sourceType, final Class<T> targetType, fin
if (value instanceof BigDecimal) {
return targetType.cast(((BigDecimal) value).toBigInteger());
}
if (value instanceof Float || value instanceof Double) {
return targetType.cast(new BigDecimal(value.toString()).toBigInteger());
}
return targetType.cast(BigInteger.valueOf(value.longValue()));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import static org.junit.jupiter.api.Assertions.assertEquals;

import java.math.BigDecimal;
import java.math.BigInteger;

import org.apache.commons.beanutils2.Converter;
Expand Down Expand Up @@ -62,6 +63,15 @@ public void tearDown() throws Exception {
converter = null;
}

@Test
void testLargeFloatingPointMagnitude() {
// A Float or Double beyond long range must keep its magnitude instead of saturating to Long.MAX_VALUE.
final Double bigDouble = Double.valueOf(1.0e30);
assertEquals(new BigDecimal(bigDouble.toString()).toBigInteger(), converter.convert(BigInteger.class, bigDouble));
final Float bigFloat = Float.valueOf(1.0e20f);
assertEquals(new BigDecimal(bigFloat.toString()).toBigInteger(), converter.convert(BigInteger.class, bigFloat));
}

@Test
void testSimpleConversion() throws Exception {
final String[] message = { "from String", "from String", "from String", "from String", "from String", "from String", "from String", "from Byte",
Expand Down
Loading