[JAVA-2140] Add support for Decimal128 BSON type Created: 07/Mar/16  Updated: 27/May/22  Resolved: 18/Jul/16

Status: Closed
Project: Java Driver
Component/s: BSON
Affects Version/s: None
Fix Version/s: 3.4.0

Type: New Feature Priority: Major - P3
Reporter: Rathi Gnanasekaran Assignee: Jeffrey Yemin
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Depends
depends on SERVER-1393 Support decimal numbers Closed
is depended on by DRIVERS-288 Decimal 128 type support Closed
Epic Link: MongoDB 3.4 for Java driver
Server Compat: 3.3

 Description   

Add driver support for a new BSON type for decimal numbers, which will be added to MongoDB in version 3.4.

This type will use the decimal128 format as defined by the IEEE 754 standard and support up to 34 decimal digits and a range from -9.999999999999999999999999999999999×10^6144 to +9.999999999999999999999999999999999×10^6144. It also supports positive and negative zero, positive and negative infinity, and two forms of NaN (regular and signaled).

Decimal values will be treated like any other numeric type, and compare and sort correctly with other types based on actual numeric value. Operations on decimals are implemented in accordance with the standard, so a value of 0.10 will retain its trailing zeros while comparing equal to 0.1, 0.10000 etc.



 Comments   
Comment by Raviteja [ 09/Jan/17 ]

I was actually transforming RDMS to Mongo over a custom code.

Reading data over JDBC and creating a JSON map using JsonBuilder and then doing a

new Document(json)

To workaround this issue I did this, FYI:

Document.parse(json.toString())

I don't want to go over each element and convert it.

Comment by Jeffrey Yemin [ 09/Jan/17 ]

bond

It seems like you're doing something like this:

   new Document("d" new BigDecimal("42.0")

If so, the driver doesn't support encoding a BigDecimal directly to Decimal128. You can do this instead:

   new Document("d" new Decimal128(new BigDecimal("42.0"))

Comment by Raviteja [ 09/Jan/17 ]

@jeff.yemin I get the following stacktrace when using the driver version 3.4.1, is this related?

Exception in thread "main" org.bson.codecs.configuration.CodecConfigurationException: Can't find a codec for class java.math.BigDecimal.
	at org.bson.codecs.configuration.CodecCache.getOrThrow(CodecCache.java:46)
	at org.bson.codecs.configuration.ProvidersCodecRegistry.get(ProvidersCodecRegistry.java:63)
	at org.bson.codecs.configuration.ProvidersCodecRegistry.get(ProvidersCodecRegistry.java:37)
	at org.bson.codecs.DocumentCodec.writeValue(DocumentCodec.java:174)
	at org.bson.codecs.DocumentCodec.writeMap(DocumentCodec.java:189)
	at org.bson.codecs.DocumentCodec.encode(DocumentCodec.java:131)
	at org.bson.codecs.DocumentCodec.encode(DocumentCodec.java:45)
	at org.bson.Document.toJson(Document.java:294)
	at org.bson.Document.toJson(Document.java:268)
	at org.bson.Document.toJson(Document.java:255)

Comment by Githook User [ 06/Sep/16 ]

Author:

{u'username': u'jyemin', u'name': u'Jeff Yemin', u'email': u'jeff.yemin@10gen.com'}

Message: JAVA-2140: Add toString method to BsonDecimal128
Branch: master
https://github.com/mongodb/mongo-java-driver/commit/87b8a21ecad3d1c98af7f622ec849736e11b2ccb

Comment by Githook User [ 18/Jul/16 ]

Author:

{u'username': u'jyemin', u'name': u'Jeff Yemin', u'email': u'jeff.yemin@10gen.com'}

Message: JAVA-2140: Support the BSON Decimal128 type
Branch: master
https://github.com/mongodb/mongo-java-driver/commit/1df96cad619af05ca1cc99963a835deffb989322

Comment by Githook User [ 26/Apr/16 ]

Author:

{u'username': u'jyemin', u'name': u'Jeff Yemin', u'email': u'jeff.yemin@10gen.com'}

Message: JAVA-2140: Prefer constructors to static factory methods, except for String conversion, which is now called parse, and conversion from high and low, which is now called fromIEEE754BIDEncoding
Branch: decimal
https://github.com/mongodb/mongo-java-driver/commit/e3b3a07483c329a0483832490880ecd31b222c40

Comment by Githook User [ 26/Apr/16 ]

Author:

{u'username': u'jyemin', u'name': u'Jeff Yemin', u'email': u'jeff.yemin@10gen.com'}

Message: JAVA-2140: Document semantics of Decimal128.equals
Branch: decimal
https://github.com/mongodb/mongo-java-driver/commit/c2354944ec330c4666c1d1259343eeb352880754

Comment by Jeffrey Yemin [ 13/Apr/16 ]

Quick tours for new and legacy APIs:

Generated at Thu Feb 08 08:56:26 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.