Skip to content

Commit ec7bb60

Browse files
committed
read double value by BigDecimal
1 parent ba6babd commit ec7bb60

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

Diff for: src/main/java/com/jsoniter/IterImpl.java

+8-4
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
package com.jsoniter;
22

3+
import java.io.IOException;
4+
import java.math.BigDecimal;
5+
import java.math.BigInteger;
6+
37
import com.jsoniter.any.Any;
48
import com.jsoniter.spi.JsonException;
59
import com.jsoniter.spi.Slice;
610

7-
import java.io.IOException;
8-
import java.math.BigInteger;
9-
1011
class IterImpl {
1112

1213
private static BigInteger maxLong = BigInteger.valueOf(Long.MAX_VALUE);
@@ -469,7 +470,10 @@ static final double readDouble(final JsonIterator iter) throws IOException {
469470
decimalPart = -decimalPart;
470471
int decimalPlaces = iter.head - start;
471472
if (decimalPlaces > 0 && decimalPlaces < IterImplNumber.POW10.length && (iter.head - oldHead) < 10) {
472-
return value + (decimalPart / (double) IterImplNumber.POW10[decimalPlaces]);
473+
BigDecimal integerPart = new BigDecimal(value);
474+
BigDecimal fractionalPart = new BigDecimal((decimalPart / (double) IterImplNumber.POW10[decimalPlaces]));
475+
BigDecimal result = integerPart.add(fractionalPart).setScale(decimalPlaces,BigDecimal.ROUND_HALF_UP);
476+
return result.doubleValue();
473477
} else {
474478
iter.head = oldHead;
475479
return IterImplForStreaming.readDoubleSlowPath(iter);

0 commit comments

Comments
 (0)