Skip to content

Commit

Permalink
bug fix for parse double value, for issue #959
Browse files Browse the repository at this point in the history
  • Loading branch information
wenshao committed Nov 24, 2022
1 parent 36419b4 commit 5e7a0d5
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
7 changes: 6 additions & 1 deletion core/src/main/java/com/alibaba/fastjson2/JSONReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -2387,11 +2387,16 @@ public Number getNumber() {
BigInteger bigInt = getBigInt(negative, mag);
int adjustedScale = scale - exponent;
decimal = new BigDecimal(bigInt, adjustedScale);

if (exponent != 0) {
return decimal.doubleValue();
}
}

if (exponent != 0) {
String decimalStr = decimal.toPlainString();
double doubleValue = Double.parseDouble(
decimal + "E" + exponent);
decimalStr + "E" + exponent);
return Double.valueOf(doubleValue);
}

Expand Down
16 changes: 16 additions & 0 deletions core/src/test/java/com/alibaba/fastjson2/issues/Issue959.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.alibaba.fastjson2.issues;

import com.alibaba.fastjson2.JSON;
import org.junit.jupiter.api.Test;

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

public class Issue959 {
@Test
public void test() {
assertEquals(
-2.0089457919266330204e-15,
JSON.parseObject("{\"V\": -2.0089457919266330204e-15}").get("V")
);
}
}

0 comments on commit 5e7a0d5

Please # to comment.