rapidJson输出时 保留小数位

pretty_writer.SetMaxDecimalPlaces(4);

这个真好用,它使用gresu, 尽可能给你处理最接近的精度,并按要求输出小数位。


#include <string>
#include <fstream>
#include <iostream>

#include <QMessageBox>
#include <QString>

#include "rapidjson/document.h"
#include "rapidjson/filereadstream.h"
#include "rapidjson/filewritestream.h"
#include "rapidjson/prettywriter.h"
#include "rapidjson/stringbuffer.h"

#include "ui_widget.h"
#include "widget.h"

using namespace std;
using namespace rapidjson;

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);
}

Widget::~Widget()
{
    delete ui;
}

void json_write();
void json_read();

void Widget::on_pushButton_clicked()
{
    //写、读 测试
    json_write();
    json_read();
}

float prec(double d, int pre = 2)
{
    QString s = QString::number(d, 'g', pre);
    return s.toFloat();
}
#undef prec
#define prec

//写json文件
void json_write()
{
    Document doc;
    doc.SetObject();
    Document::AllocatorType &allocator=doc.GetAllocator(); //获取分配器
    //1.添加字符串对象
    doc.AddMember("author","tashaxing",allocator);
    //2.添加数组对象
    Value array1(kArrayType);
    for(int i=0;i<3;i++)
    {
        Value int_object(kObjectType);
        int_object.SetInt(i);
        array1.PushBack(int_object,allocator);
    }
    doc.AddMember("number",array1,allocator);
    //3.添加复合对象
    Value object(kObjectType);
    object.AddMember("language1","C++",allocator);
    object.AddMember("language2","java",allocator);
    doc.AddMember("language",object,allocator);
    //4.添加对象数组和复合对象的组合
    Value array2(kArrayType);
    Value object1(kObjectType);
    object1.AddMember("hobby","drawing",allocator);
    array2.PushBack(object1,allocator);
    Value object2(kObjectType);
    object2.AddMember("height",prec(-12941.71999999999993),allocator);
    array2.PushBack(object2,allocator);
    doc.AddMember("information",array2,allocator);
    StringBuffer buffer;
    PrettyWriter<StringBuffer> pretty_writer(buffer);  //PrettyWriter是格式化的json,如果是Writer则是换行空格压缩后的json
    pretty_writer.SetMaxDecimalPlaces(4);
    doc.Accept(pretty_writer);
    //打印到屏幕
    cout<<"the json output:"<<endl;
    cout<<buffer.GetString()<<endl;
    //输出到文件
    ofstream fout;
    fout.open("test");    //可以使绝对和相对路径,用\\隔开目录,test, test.json, test.txt 都行,不局限于文件格式后缀,只要是文本文档
    fout<<buffer.GetString();
    fout.close();
}

//读json文件
void json_read()
{
    cout<<"the json read:"<<endl;
    ifstream fin;
    fin.open("test");
    string str;
    string str_in="";
    while(getline(fin,str))    //一行一行地读到字符串str_in中
    {
        str_in=str_in+str+'\n';
    }
    //解析并打印出来
    Document document;
    document.Parse<0>(str_in.c_str());

    Value &node1=document["author"];
    cout<<"author: "<<node1.GetString()<<endl;

    Value &node2=document["number"];
    cout<<"number: "<<endl;
    if(node2.IsArray())
    {
        for(int i=0;i<node2.Size();i++)
            cout<<'\t'<<node2[i].GetInt()<<endl;
    }

    Value &node3=document["language"];
    cout<<"language: "<<endl;
    Value &tmp=node3["language1"];
    cout<<'\t'<<"language1: "<<tmp.GetString()<<endl;
    tmp=node3["language2"];
    cout<<'\t'<<"language2: "<<tmp.GetString()<<endl;

    Value &node4=document["information"];
    cout<<"information: "<<endl;
    if(node4.IsArray())
    {
        int i=0;
        Value &data=node4[i];   //注意,此处下表索引只能用变量,不能用常量,例如node[0]编译错误
        cout<<'\t'<<"hobby: "<<data["hobby"].GetString()<<endl;
        i=1;
        data=node4[i];
        cout<<'\t'<<"height: "<<data["height"].GetDouble()<<endl;
    }

}

d参考:

https://gmplib.org/

https://www.cnblogs.com/miloyip/p/4610111.html


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值