| 162 | |
| 163 | // Special versions for floats/doubles. |
| 164 | template<typename T> std::string FloatToString(T t, int precision) { |
| 165 | // clang-format off |
| 166 | |
| 167 | #ifndef FLATBUFFERS_PREFER_PRINTF |
| 168 | // to_string() prints different numbers of digits for floats depending on |
| 169 | // platform and isn't available on Android, so we use stringstream |
| 170 | std::stringstream ss; |
| 171 | // Use std::fixed to suppress scientific notation. |
| 172 | ss << std::fixed; |
| 173 | // Default precision is 6, we want that to be higher for doubles. |
| 174 | ss << std::setprecision(precision); |
| 175 | ss << t; |
| 176 | auto s = ss.str(); |
| 177 | #else // FLATBUFFERS_PREFER_PRINTF |
| 178 | auto v = static_cast<double>(t); |
| 179 | auto s = NumToStringImplWrapper(v, "%0.*f", precision); |
| 180 | #endif // FLATBUFFERS_PREFER_PRINTF |
| 181 | // clang-format on |
| 182 | // Sadly, std::fixed turns "1" into "1.00000", so here we undo that. |
| 183 | auto p = s.find_last_not_of('0'); |
| 184 | if (p != std::string::npos) { |
| 185 | // Strip trailing zeroes. If it is a whole number, keep one zero. |
| 186 | s.resize(p + (s[p] == '.' ? 2 : 1)); |
| 187 | } |
| 188 | return s; |
| 189 | } |
| 190 | |
| 191 | template<> inline std::string NumToString<double>(double t) { |
| 192 | return FloatToString(t, 12); |
no test coverage detected