MCPcopy Create free account
hub / github.com/catboost/catboost / FloatToString

Function FloatToString

contrib/libs/flatbuffers/include/flatbuffers/util.h:153–178  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

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

Callers 15

NumToString<double>Function · 0.70
NumToString<float>Function · 0.70
DeserializeMethod · 0.50
SaveInMatrixnetFormatFunction · 0.50
FinishIterationBlockMethod · 0.50
FloatToStringWithSuffixFunction · 0.50
OutputLeafValuesFunction · 0.50
WriteModelCatFeaturesMethod · 0.50
WriteModelMethod · 0.50
EvaluateFeaturesImplFunction · 0.50
OutputProfileMethod · 0.50

Calls 4

NumToStringImplWrapperFunction · 0.85
strMethod · 0.45
find_last_not_ofMethod · 0.45
resizeMethod · 0.45

Tested by

no test coverage detected