3

I am trying to get double type data from the database as the documentation says:

auto cursor = db["collection"].find({}, opts);
for (auto&& doc : cursor) {
    std::cout << bsoncxx::to_json(doc) << std::endl;
}

But I want to avoid converting doc into json because I lost decimal precision. For example:

In database shows this:

"lng" : -58.4682568037741

But after converting to json, I receive this:

"lng" : -58.4682

Is there any way to convert it directly to string, for example?

acm
  • 12,183
  • 5
  • 39
  • 68
debihiga
  • 246
  • 2
  • 10

1 Answers1

4

You can pull out the field you want directly as a double. To print high-precision output, you need to set that on the output stream. E.g.

for (auto&& doc : cursor) {
    std::cout << std::setprecision(15)
              << "lng: " << doc["lng"].get_double() << std::endl;
}

Gives:

lng: -58.4682568037741

You may want to verify that doc["lng"] is a BSON double before calling get_double.

xdg
  • 2,975
  • 19
  • 17