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 @@ -279,7 +279,8 @@ public static String printDecimal(BigDecimal value) {
// The following code comes from Apache Harmony
String intStr = value.unscaledValue().toString();
int scale = value.scale();
if (scale == 0 || (MathUtil.toLong(value) == 0 && scale < 0)) {
// the second branch only needs a zero check, as in the original Harmony code
if (scale == 0 || (value.signum() == 0 && scale < 0)) {
return intStr;
}

Expand Down
25 changes: 25 additions & 0 deletions src/test/java/misc/checkin/XsTypeConverterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import org.apache.xmlbeans.impl.values.XmlValueOutOfRangeException;
import org.junit.jupiter.api.Test;

import java.math.BigDecimal;

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

Expand Down Expand Up @@ -309,4 +311,27 @@ public java.util.Iterator<String> getPrefixes(String uri) {
assertEquals("", empty.getLocalPart());
assertEquals(1, errors.size());
}

@Test
void printDecimalHandlesValuesWiderThanLong() {
assertEquals("123456789012345678901234567890.5",
XsTypeConverter.printDecimal(new BigDecimal("123456789012345678901234567890.5")));
assertEquals("-123456789012345678901234567890.5",
XsTypeConverter.printDecimal(new BigDecimal("-123456789012345678901234567890.5")));
}

@Test
void printDecimalHandlesNegativeScaleWiderThanLong() {
// stripTrailingZeros() and BigDecimal.valueOf(unscaled, negativeScale) both
// produce negative-scale values, which reach printDecimal via setBigDecimalValue.
assertEquals("100000000000000000000", XsTypeConverter.printDecimal(new BigDecimal("1E+20")));
assertEquals("-100000000000000000000", XsTypeConverter.printDecimal(new BigDecimal("-1E+20")));
assertEquals("100", XsTypeConverter.printDecimal(new BigDecimal("1E+2")));
}

@Test
void printDecimalHandlesZeroWithScale() {
assertEquals("0", XsTypeConverter.printDecimal(new BigDecimal("0E+5")));
assertEquals("0.00000", XsTypeConverter.printDecimal(new BigDecimal("0E-5")));
}
}