|
23 | 23 |
|
24 | 24 | import java.math.BigDecimal; |
25 | 25 | import java.math.BigInteger; |
| 26 | +import java.math.MathContext; |
26 | 27 |
|
27 | 28 | import static java.util.Objects.requireNonNull; |
28 | 29 |
|
@@ -282,6 +283,13 @@ public final DecimalFormat getDecimalFormat() { |
282 | 283 | return decimalFormat; |
283 | 284 | } |
284 | 285 |
|
| 286 | + /** |
| 287 | + * @return Math context for the decimal type constructed. |
| 288 | + */ |
| 289 | + private MathContext getMathContext() { |
| 290 | + return decimalFormat.getMathContext(); |
| 291 | + } |
| 292 | + |
285 | 293 | /** |
286 | 294 | * @see DecimalFormat#validate(BigDecimal) |
287 | 295 | */ |
@@ -332,7 +340,7 @@ final T valueOf(BigDecimal value, OverflowHandling overflowHandling) { |
332 | 340 | * @see #valueOfExact(BigInteger) |
333 | 341 | */ |
334 | 342 | final T valueOf(BigInteger value, OverflowHandling overflowHandling) { |
335 | | - return valueOf(new BigDecimal(value), overflowHandling); |
| 343 | + return valueOf(new BigDecimal(value, getMathContext()), overflowHandling); |
336 | 344 | } |
337 | 345 |
|
338 | 346 | /** |
@@ -375,7 +383,8 @@ final T valueOf(double value, OverflowHandling overflowHandling) { |
375 | 383 | return getSpecialConstant(Signum.NEGATIVE, DecimalType.INFINITY); |
376 | 384 | } |
377 | 385 |
|
378 | | - return valueOf(BigDecimal.valueOf(value), overflowHandling); |
| 386 | + // TODO Use new BigDecimal(double, MathContext) instead, has slightly different precision? |
| 387 | + return valueOf(new BigDecimal(Double.toString(value), getMathContext()), overflowHandling); |
379 | 388 | } |
380 | 389 |
|
381 | 390 | /** |
@@ -436,7 +445,7 @@ final T valueOf(String value, OverflowHandling overflowHandling) { |
436 | 445 | return valueOfSpecial(value); |
437 | 446 | } |
438 | 447 | } |
439 | | - BigDecimal bdValue = new BigDecimal(value); |
| 448 | + BigDecimal bdValue = new BigDecimal(value, getMathContext()); |
440 | 449 | T decimalValue = valueOf(bdValue, overflowHandling); |
441 | 450 | if (decimalValue.isEquivalentToZero() |
442 | 451 | && value.charAt(0) == '-' |
|
0 commit comments