0

I have little experience with MongoDB. I am usually working on SQL DBs. I tried to get a double value from MongoDB using c# driver.

Data ==> {"Reading":20.15}

Query ==> 
ProjectionDefinition<BsonDocument> project = Builders<BsonDocument>.Projection.Include("Reading").Exclude("_id");
FilterDefinition<BsonDocument> filterDefintion = Builders<BsonDocument>.Filter.Eq("Company","companyname");
collection.Find(filterDefintion).Sort(sortDefinition).Project(project).FirstOrDefault();

Result ==> { "Reading" : 20.149999999999999 }

how to get exact value 20.15 from MongoDB ??

Thanks for your help

leo Martin
  • 180
  • 1
  • 3
  • 15
  • How is it totally different? Can you explain how to represent 0.15 in binary? – ProgrammingLlama Nov 12 '18 at 07:19
  • 1
    Moreover there is a "MongoDB specific" variant of that longer and generic explanation, which basically says "Don't use **Double** for this purpose". There are other types and better ways if floating point precision is something you need. – Neil Lunn Nov 12 '18 at 07:20
  • Tetsuya Yamamoto I don't understand why these question marked as duplicate – leo Martin Nov 12 '18 at 07:28
  • @NeilLunn .Is it any way to get double value with precision – leo Martin Nov 12 '18 at 07:29
  • @arun Go [here](https://www.exploringbinary.com/floating-point-converter/) and check "Decimal" and "Binary" under "Outputs". Then enter your 20.15 and click "Convert". You will see that 20.15 cannot be represented in binary, and thus is a repeating `.0010011` forever. Because there is limited storage space available, this eventually stops and you get the closest approximate value available. (This _is explained_ in depth in the duplicate) – ProgrammingLlama Nov 12 '18 at 07:41
  • If it's easier to think about: you can't precisely represent 1/3 in decimal. You have to stop at an acceptable level of precision, right? Computers are limited by their architecture. – ProgrammingLlama Nov 12 '18 at 07:48

0 Answers0