mirror of
https://github.com/google/flatbuffers.git
synced 2026-06-26 16:52:40 +00:00
Made doubles output with higher precision in JSON and elsewhere.
Change-Id: I5dd8f3a7c315c40b86a67aba5c2cca4d36363523 Tested: on Linux.
This commit is contained in:
@@ -76,12 +76,15 @@ inline std::string NumToString<unsigned long long>(unsigned long long t) {
|
|||||||
#endif // defined(FLATBUFFERS_CPP98_STL)
|
#endif // defined(FLATBUFFERS_CPP98_STL)
|
||||||
|
|
||||||
// Special versions for floats/doubles.
|
// Special versions for floats/doubles.
|
||||||
template<> inline std::string NumToString<double>(double t) {
|
template<typename T> std::string FloatToString(T t, int precision) {
|
||||||
// to_string() prints different numbers of digits for floats depending on
|
// to_string() prints different numbers of digits for floats depending on
|
||||||
// platform and isn't available on Android, so we use stringstream
|
// platform and isn't available on Android, so we use stringstream
|
||||||
std::stringstream ss;
|
std::stringstream ss;
|
||||||
// Use std::fixed to surpress scientific notation.
|
// Use std::fixed to surpress scientific notation.
|
||||||
ss << std::fixed << t;
|
ss << std::fixed;
|
||||||
|
// Default precision is 6, we want that to be higher for doubles.
|
||||||
|
ss << std::setprecision(precision);
|
||||||
|
ss << t;
|
||||||
auto s = ss.str();
|
auto s = ss.str();
|
||||||
// Sadly, std::fixed turns "1" into "1.00000", so here we undo that.
|
// Sadly, std::fixed turns "1" into "1.00000", so here we undo that.
|
||||||
auto p = s.find_last_not_of('0');
|
auto p = s.find_last_not_of('0');
|
||||||
@@ -91,8 +94,12 @@ template<> inline std::string NumToString<double>(double t) {
|
|||||||
}
|
}
|
||||||
return s;
|
return s;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template<> inline std::string NumToString<double>(double t) {
|
||||||
|
return FloatToString(t, 12);
|
||||||
|
}
|
||||||
template<> inline std::string NumToString<float>(float t) {
|
template<> inline std::string NumToString<float>(float t) {
|
||||||
return NumToString(static_cast<double>(t));
|
return FloatToString(t, 6);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Convert an integer value to a hexadecimal string.
|
// Convert an integer value to a hexadecimal string.
|
||||||
|
|||||||
@@ -3,7 +3,7 @@
|
|||||||
x: 1.0,
|
x: 1.0,
|
||||||
y: 2.0,
|
y: 2.0,
|
||||||
z: 3.0,
|
z: 3.0,
|
||||||
test1: 3.0,
|
test1: 3.14159265359,
|
||||||
test2: "Green",
|
test2: "Green",
|
||||||
test3: {
|
test3: {
|
||||||
a: 10,
|
a: 10,
|
||||||
|
|||||||
Reference in New Issue
Block a user