@@ -157,27 +157,43 @@ inline std::string NumToString<char>(char t) {
157157
158158// definitions in util.cpp
159159template <typename Float>
160- std::string format_fixed_dragonbox (Float value);
160+ std::string FloatToStringImpl (Float value);
161161
162162// Special versions for floats/doubles.
163163template <typename T>
164164std::string FloatToString (T t, [[maybe_unused]] int precision) {
165165 // clang-format off
166166
167167 #ifndef FLATBUFFERS_PREFER_PRINTF
168- return format_fixed_dragonbox (t);
168+ #ifndef FLATBUFFERS_PREFER_OLD_FLOATTOSTRING
169+ return FloatToStringImpl (t);
170+ #else // FLATBUFFERS_PREFER_OLD_FLOATTOSTRING
171+ // to_string() prints different numbers of digits for floats depending on
172+ // platform and isn't available on Android, so we use stringstream
173+ std::stringstream ss;
174+ // Use std::fixed to suppress scientific notation.
175+ ss << std::fixed;
176+ // Default precision is 6, we want that to be higher for doubles.
177+ ss << std::setprecision (precision);
178+ ss << t;
179+ auto s = ss.str ();
180+ #endif // FLATBUFFERS_PREFER_OLD_FLOATTOSTRING
169181 #else // FLATBUFFERS_PREFER_PRINTF
170182 auto v = static_cast <double >(t);
171183 auto s = NumToStringImplWrapper (v, " %0.*f" , precision);
172- // clang-format on
184+ #endif // FLATBUFFERS_PREFER_PRINTF
185+
186+ #if defined(FLATBUFFERS_PREFER_OLD_FLOATTOSTRING) || defined(FLATBUFFERS_PREFER_PRINTF)
173187 // Sadly, std::fixed turns "1" into "1.00000", so here we undo that.
174188 auto p = s.find_last_not_of (' 0' );
175189 if (p != std::string::npos) {
176190 // Strip trailing zeroes. If it is a whole number, keep one zero.
177191 s.resize (p + (s[p] == ' .' ? 2 : 1 ));
178192 }
193+
179194 return s;
180- #endif // FLATBUFFERS_PREFER_PRINTF
195+ #endif
196+ // clang-format on
181197}
182198
183199template <>
0 commit comments