-
Type:
Bug
-
Resolution: Gone away
-
Priority:
Minor - P4
-
None
-
Affects Version/s: 4.11.5, 5.3.0
-
Component/s: BSON
-
None
-
None
-
Java Drivers
-
None
-
None
-
None
-
None
-
None
-
None
Summary
I want to get the value of the number type data, there is no problem in int32, int64, decimal, but when using the double type, he has NumberFormatException exception
Exception in thread "main" java.lang.NumberFormatException: Conversion to Decimal128 would require inexact rounding of 12.1199999999999992184029906638897955417633056640625
at org.bson.types.Decimal128.ensureExactRounding(Decimal128.java:244)
at org.bson.types.Decimal128.clampAndRound(Decimal128.java:233)
at org.bson.types.Decimal128.<init>(Decimal128.java:169)
at org.bson.types.Decimal128.<init>(Decimal128.java:156)
at org.bson.BsonDouble.decimal128Value(BsonDouble.java:79)
took a look and found that there is a problem with the decimal128Value method. The correct one should be: new BigDecimal (Double.toString (this.value))
public Decimal128 decimal128Value() { if (Double.isNaN(this.value)) { return Decimal128.NaN; } else if (Double.isInfinite(this.value)) { return this.value > 0.0 ? Decimal128.POSITIVE_INFINITY : Decimal128.NEGATIVE_INFINITY; } else { return new Decimal128(new BigDecimal(this.value)); } }
Please provide the version of the driver. If applicable, please provide the MongoDB server version and topology (standalone, replica set, or sharded cluster).
4.11.2
How to Reproduce
BsonDouble bsonDouble = new BsonDouble(12.12); if (bsonDouble.isNumber()){ System.out.println(bsonDouble.asNumber().decimal128Value()); }
Additional Background
Please provide any additional background information that may be helpful in diagnosing the bug.