MCPcopy Create free account
hub / github.com/bytedance/Fastbot_Android / FloatToString

Function FloatToString

native/thirdpart/flatbuffers/util.h:164–189  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

162
163// Special versions for floats/doubles.
164template<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
191template<> inline std::string NumToString<double>(double t) {
192 return FloatToString(t, 12);

Callers 3

DeserializeMethod · 0.85
NumToString<double>Function · 0.85
NumToString<float>Function · 0.85

Calls 2

NumToStringImplWrapperFunction · 0.85
strMethod · 0.45

Tested by

no test coverage detected