0

I am using MongoDB to do some calculations as per below. Creating one collection called "Numbers" Insert a big number let's say 2^64 ( we can store this big as MongoDB support decimal128 as default data type)

// inserting  
db.numbers.insertOne({newBig: 1298074200000000000000000000000000})
// converting 
db.numbers.aggregate({$addFields:{test: {$toString: "$newBig"}}})

Now when I am converting the above number to a string, it's not giving expected results.

// getting following output 
[{
    _id: ObjectId("61c2c1557d78b0f039bcee5a"),
    newBig: 1.2980742e+33,
    test: '1.29807e+33'
}]

As pointers, M I missing something here? Please acknowledge, thanks :)

Xeelley
  • 1,081
  • 2
  • 8
  • 18
  • I am not sure if I am understanding correctly what do you mean by *missing* ? If you are talking about the `42`, I would make a bold declaration that it makes no sense to compare a int64 value with a string representation. If you need to keep the precision, just perform the computation on the raw numeric value. Leave the manipulation of string representation to the presentation layer(i.e. the web frontend/BI report...) – ray Dec 22 '21 at 09:51
  • Check out this question: https://stackoverflow.com/questions/18719775/parsing-and-converting-exponential-values-to-decimal-in-javascript – Xeelley Dec 29 '21 at 15:58

0 Answers0