Skip to content

Commit

Permalink
fix UseBigDecimalForFloats, for issue #2866
Browse files Browse the repository at this point in the history
  • Loading branch information
wenshao committed Aug 25, 2024
1 parent 9fd7031 commit c9b2559
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 130 deletions.
58 changes: 2 additions & 56 deletions core/src/main/java/com/alibaba/fastjson2/JSONReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -2873,51 +2873,6 @@ public final Number getNumber() {
if (mag0 == 0 && mag1 == 0) {
if (mag2 == 0 && mag3 >= 0) {
int unscaledVal = negative ? -mag3 : mag3;

if (exponent == 0) {
if ((context.features & Feature.UseBigDecimalForFloats.mask) != 0) {
switch (scale) {
case 1:
case 2:
case 3:
case 4:
case 5:
case 6:
case 7:
case 8:
case 9:
case 10:
return (float) (unscaledVal / DOUBLE_10_POW[scale]);
default:
break;
}
} else if ((context.features & Feature.UseBigDecimalForDoubles.mask) != 0) {
if (unscaledVal == 0) {
return DOUBLE_ZERO;
}

switch (scale) {
case 1:
case 2:
case 3:
case 4:
case 5:
case 6:
case 7:
case 8:
case 9:
case 10:
case 11:
case 12:
case 13:
case 14:
case 15:
return unscaledVal / DOUBLE_10_POW[scale];
default:
break;
}
}
}
decimal = BigDecimal.valueOf(unscaledVal, scale);
} else {
long v3 = mag3 & 0XFFFFFFFFL;
Expand Down Expand Up @@ -3008,26 +2963,17 @@ public final Number getNumber() {

int adjustedScale = scale - exponent;
decimal = new BigDecimal(bigInt, adjustedScale);

if (exponent != 0) {
if (exponent != 0 && (context.features & (Feature.UseBigDecimalForDoubles.mask | Feature.UseBigDecimalForFloats.mask)) == 0) {
return decimal.doubleValue();
}
}

if (exponent != 0) {
if (exponent != 0 && (context.features & (Feature.UseBigDecimalForDoubles.mask | Feature.UseBigDecimalForFloats.mask)) == 0) {
String decimalStr = decimal.toPlainString();
return Double.parseDouble(
decimalStr + "E" + exponent);
}

if ((context.features & Feature.UseBigDecimalForFloats.mask) != 0) {
return decimal.floatValue();
}

if ((context.features & Feature.UseBigDecimalForDoubles.mask) != 0) {
return decimal.doubleValue();
}

return decimal;
}
case JSON_TYPE_BIG_DEC: {
Expand Down
Loading

0 comments on commit c9b2559

Please sign in to comment.