JsonCpp中double的問題解決
json文件里的值

程序代碼
new_item["Voltage"] = 8.622; new_item["Current"] = 8.63456; new_item["Energy"] = 8.234234;
程序運(yùn)行結(jié)果

Jsoncpp的json_write.cpp中
std::string valueToString(double value, bool useSpecialFloats, unsigned int precision) {
// Allocate a buffer that is more than large enough to store the 16 digits of
// precision requested below.
char buffer[32];
int len = -1;
char formatString[6];
sprintf(formatString, "%%.%dg", precision);
// Print into the buffer. We need not request the alternative representation
// that always has a decimal point because JSON doesn't distingish the
// concepts of reals and integers.
if (isfinite(value)) {
len = snprintf(buffer, sizeof(buffer), formatString, value);
} else {
// IEEE standard states that NaN values will not compare to themselves
if (value != value) {
len = snprintf(buffer, sizeof(buffer), useSpecialFloats ? "NaN" : "null");
} else if (value < 0) {
len = snprintf(buffer, sizeof(buffer), useSpecialFloats ? "-Infinity" : "-1e+9999");
} else {
len = snprintf(buffer, sizeof(buffer), useSpecialFloats ? "Infinity" : "1e+9999");
}
// For those, we do not need to call fixNumLoc, but it is fast.
}
assert(len >= 0);
fixNumericLocale(buffer, buffer + len);
return buffer;
}
這里sprintf(formatString, “%%.%dg”, precision);的結(jié)果是“%.17g”。是輸出17位的有效數(shù)字。不足的補(bǔ)足17位。
ps:JsonCpp的小數(shù)精度問題和插入輸出順序問題
直接說吧,這兩個問題無法解決,如下:
官方不支持指定小數(shù)位數(shù),double默認(rèn)位寬為17位,如:"value" : 7.0999999999999996,
官方不支持按插入順序輸出,而是按照key的字母排序輸出的,不管你什么順序插入,下面的都是這樣的順序輸出的:
"avg_abcdd " : 1.1632640000000014, "avg_pxczzczxczxd " : 7.0999999999999996, "avg_shczxcdize " : 802000.0, "deviccxz " : "shebei25", "sh323423fd " : 1420, "vcxzcasdasdadczco " : 231
個人應(yīng)急想法
數(shù)字精度問題,可以考慮在C++中轉(zhuǎn)為自己需求的精度,然后再當(dāng)作字符串放到j(luò)son中,至于之后的解析,讀字符串再轉(zhuǎn)數(shù)字即可;
順序問題,兩個想法:
1)不要用key,采用append的形式,也就是將每個條目放在一個容器中
Json::Value res;?
?
std::string = entry_str;?
?
entry_str.append("zhangsan,123");?
entry_str.append("abc,2596");?
.......?
?
res["entry"] = entry_str;2)那就按名字命令咯,順應(yīng)規(guī)則,2333333
到此這篇關(guān)于JsonCpp中double的問題解決的文章就介紹到這了,更多相關(guān)JsonCpp double內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Opencv3.4.0實(shí)現(xiàn)視頻中的幀保存為圖片功能
這篇文章主要為大家詳細(xì)介紹了Opencv3.4.0實(shí)現(xiàn)視頻中的幀保存為圖片功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-05-05
C語言中fchdir()函數(shù)和rewinddir()函數(shù)的使用詳解
這篇文章主要介紹了C語言中fchdir()函數(shù)和rewinddir()函數(shù)的使用詳解,是C語言入門學(xué)習(xí)中的基礎(chǔ)知識,需要的朋友可以參考下2015-09-09
C++ GDI實(shí)現(xiàn)圖片格式轉(zhuǎn)換
GDI+(Graphics Device Interface Plus)是一種用于圖形繪制和圖像處理的應(yīng)用程序編程接口(API),在Windows平臺上廣泛使用,本文就來介紹一下如何使用GDI實(shí)現(xiàn)圖片格式轉(zhuǎn)換吧2023-12-12
C語言實(shí)現(xiàn)linux網(wǎng)卡檢測精簡版
這篇文章主要為大家詳細(xì)介紹了C語言實(shí)現(xiàn)linux網(wǎng)卡檢測的精簡版,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-06-06
C/C++詳解實(shí)現(xiàn)二層轉(zhuǎn)發(fā)
數(shù)據(jù)鏈路層是開放系統(tǒng)互連 (OSI) 模型中的第二層,該層用于通過 LAN 等單一網(wǎng)絡(luò)進(jìn)行通信的節(jié)點(diǎn),第二層數(shù)據(jù)包不能從一個網(wǎng)絡(luò)傳輸?shù)搅硪粋€網(wǎng)絡(luò)。而二層轉(zhuǎn)發(fā)是根據(jù)報文的目的MAC直接進(jìn)行轉(zhuǎn)發(fā),轉(zhuǎn)發(fā)過程中不用對報文的頭部做任何的修改2022-05-05
C++中utf8字符串和gbk字符串的轉(zhuǎn)換方法
文章介紹了C++中UTF-8字符串和GBK字符串之間的轉(zhuǎn)換,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),感興趣的朋友跟隨小編一起看看吧2025-02-02

