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则是换行空格压缩后的jsonpretty_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

rapidJson输出时 保留小数位相关推荐

  1. rapidJson输出时保留指定小数位

    rapidJson输出时 保留小数位 //PrettyWriter是格式化的json,如果是Writer则是换行空格压缩后的json pretty_writer.SetMaxDecimalPlaces ...

  2. 输入两个数、用python求他们的和_从键盘上输入俩个实型数,编程求它们的和差积商,要求输出时,保留两位小数C语言编程题:从键盘上输入两个...

    从键盘上输入俩个实型数,编程求它们的和差积商,要求输出时,保留两位小数 C语言编程题:从键盘上输入两个 www.zhiqu.org     时间: 2020-11-23 参考代码: #includev ...

  3. 第4课 从键盘读入变量的值及保留小数位问题

    一.为什么要从键盘读入? 1000:[入门]熟悉一下Online Judge的环境 1311:[入门]分跳绳 1416:[入门]求长方形的周长和面积 1320:[入门]时针旋转(1)? 1417:[入 ...

  4. python round保留小数位_Python-其他-round()保留小数位时遇到的问题

    最近有一个需求,原有整数计算,改成小数计算,保留一位小数. 于是按照需求,将数据结构由 int 改为 float ,计算时采用round()方法来保留小数位. 第一版代码如下: a = 0.10000 ...

  5. Java 保留小数位时整数位0丢失问题

    Java 保留小数位时整数位0丢失问题 Java 保留三位小数位 推荐写法: java.text.DecimalFormat df =new java.text.DecimalFormat(" ...

  6. python输出时怎么保留两位小数_python输出怎么保留两位小数-Python教程

    python输入保存两位小数的四种办法: a = 5.5461 办法一:round(a,2) 办法二:float('%.2f' % a) 相干保举:<Python视频教程> 办法三:'%. ...

  7. 将python中的小数直接进位的函数_python保留小数位的三种实现方法

    python保留小数位的三种实现方法 前言 保留小数位是我们经常会碰到的问题,尤其是刷题过程中.那么在python中保留小数位的方法也非常多,但是笔者的原则就是什么简单用什么,因此这里介绍几种比较简单 ...

  8. c语言四舍五入任意位,js四舍五入及任意保留小数位

    在 Javascript 中,四舍五入用 Math.round() 和 toFixed() 方法实现:前者只能取到整数部分,小数全部舍弃,如果要保留小数位,还得加些辅助代码:后者可任意保留小数位.下面 ...

  9. python中保留小数_python保留小数位的三种实现方法

    前言 保留小数位是我们经常会碰到的问题,尤其是刷题过程中.那么在python中保留小数位的方法也非常多,但是笔者的原则就是什么简单用什么,因此这里介绍几种比较简单实用的保留小数位的方法: 方法一:fo ...

最新文章

  1. Size Matters! Long-Read DNA Sequencing
  2. 可扩展标记语言--XML
  3. ---随心买统计查询
  4. CSS鼠标响应事件经过、移动、点击示例介绍
  5. 响应文件是不是标书_什么是标书?投标书有哪些分类?标书和投标书的不同?...
  6. python获取历史双色球数据_你的梦想,我来买单!Python分析双色球中奖号码竟成功获取特等奖
  7. symbian 中自动寻找cmwap连接点,通杀uiq 2nd 3nd和s60 2nd 3nd 5nd
  8. 数据表的新建 修改 删除 mysql
  9. 外媒:巴基斯坦将成为南亚地区首个测试5G通讯的国家
  10. html前端验证代码,前端js+html实现简单验证码
  11. 经典坦克大战——C++实现(附源码)
  12. java文本框失去焦点事件,jQuery 文本框得失焦点的简单实例
  13. autoit临时资料——学习的部分会更新
  14. NetSuite 设置库存盘点
  15. 蓝桥杯:历年试题PREV-55—小计算器
  16. Python 音频随机播放器脚本
  17. Unity3D-VR《静夜诗》2-凝视宝剑和书籍时出现提示文本信息
  18. 性与潜能:性能量是一切天才的创造力源泉
  19. 最全国外优秀技术网站推荐
  20. Matlab 线性规划练习题

热门文章

  1. 全球最美的15座数据中心
  2. java 内嵌汇编_C6000嵌入汇编C与汇编对照及功能说明
  3. c# npoi 2.5版本设置字体加粗_巨巨巨巨推荐:SCI翻译神器,大版本更新来了
  4. DL之SqueezeNet:SqueezeNet算法的架构详解
  5. TF之LSTM:利用多层LSTM算法对MNIST手写数字识别数据集进行多分类
  6. NLP:基于textrank4zh库对文本实现提取文本关键词、文本关键短语和文本摘要
  7. DL之DNN:自定义2层神经网络TwoLayerNet模型(计算梯度两种方法)利用MNIST数据集进行训练、预测
  8. TF之AutoML之AdaNet框架:AdaNet框架的简介、特点、使用方法详细攻略
  9. bzoj3714:[PA2014]Kuglarz
  10. 作业三--简单四则运算