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)文章
利用C++ R3層斷鏈實(shí)現(xiàn)模塊隱藏功能
在R3層的模塊隱藏,我們需要做的就是將其該鏈表斷鏈,將某一模塊從這個雙向鏈表中摘除,這樣再調(diào)用傳統(tǒng)的API時就會搜索不到。本文重點(diǎn)給大家介紹利用C++ R3層斷鏈實(shí)現(xiàn)模塊隱藏功能,感興趣的朋友一起看看吧2019-10-10
C++實(shí)現(xiàn)LeetCode(6.字型轉(zhuǎn)換字符串)
這篇文章主要介紹了C++實(shí)現(xiàn)LeetCode(6.字型轉(zhuǎn)換字符串),本篇文章通過簡要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-07-07
C++實(shí)現(xiàn)PatchMatch圖像修復(fù)算法
這篇文章主要介紹了C++實(shí)現(xiàn)PatchMatch圖像修復(fù)算法,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-04-04
c++中深淺拷貝以及寫時拷貝的實(shí)現(xiàn)示例代碼
這篇文章主要給大家介紹了關(guān)于c++中深淺拷貝以及寫時拷貝實(shí)現(xiàn)的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家具有一定的參考學(xué)習(xí)價值,需要的朋友們下面跟著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2017-08-08
如何為Qt視圖中的文字實(shí)現(xiàn)彩虹漸變效果
這篇文章主要給大家介紹了關(guān)于如何為Qt視圖中的文字實(shí)現(xiàn)彩虹漸變效果的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者使用Qt具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-03-03

