bsoncxx:to_json导致双精度值被截断

时间:2019-03-29 04:03:25

标签: mongo-cxx-driver

当我使用bsoncxx :: to_json函数将准备好的bsoncxx文档转换为json时,所有float / double值均被截断为5位精度。请检查以下代码:

#include <iostream>

#include <bsoncxx/json.hpp>
#include <bsoncxx/builder/basic/array.hpp>
#include <bsoncxx/builder/stream/document.hpp>
#include <bsoncxx/builder/basic/document.hpp>

using namespace bsoncxx;
using namespace std;
using bsoncxx::builder::basic::kvp;
using bsoncxx::builder::basic::document;

int main(void) {
  bsoncxx::builder::stream::document d1;
  bsoncxx::builder::basic::document d2;
  cout.precision(17);

  double v1=1.0123456789;
  double v2=9.8765432111;

  d1<<"T1"<<v1;
  cout<<bsoncxx::to_json(d1.view())<<endl;

  d2.append(kvp("T2", types::b_double{v2}));
  cout<<bsoncxx::to_json(d2.view())<<endl;

  cout<<"V1: "<<v1<<endl;
  cout<<"V2: "<<v2<<endl;

  cout<<"T1: "<<d1.view()["T1"].get_double()<<endl;
  cout<<"T2: "<<d2.view()["T2"].get_double()<<endl;
}

示例输出:

{
    "T1" : 1.01235
}
{
    "T2" : 9.87654
}

V1: 1.0123456789
V2: 9.8765432110999996
T1: 1.0123456789
T2: 9.8765432110999996

我希望从to_json调用中获得以下输出:

{
    "T1" : 1.0123456789
}

{
    "T2" : 9.8765432110999996
}

如何解决此问题?

libbsoncxx-> 3.0.1 libbson-> 1.9.2

谢谢!

0 个答案:

没有答案