c++ - How to avoid scientific notation and show full number instead? -
i have below code in 1 of library causing numbers shown in form of scientific notation.
t value = 0; template<typename u> void process(u& buf, dataoption holder) const { if (holder == dataoption::types) { switch (type_) { case teck::proc_int: buf << "{\"int\":" << value << "}"; break; case teck::proc_long: buf << "{\"long\":" << value << "}"; break; case teck::proc_float: buf << "{\"float\":" << value << "}"; break; case teck::proc_double: buf << "{\"double\":" << value << "}"; break; default: buf << "{\"" << type_ << "\":" << value << "}"; } } }
for of different cases above, "value" coming in scientific notation. how can avoid showing scientific notation , instead show full number? did research , can use "std::fixed" should using this?
std::fixed
works within same stream, won't work in case you're working stateless stream
case teck::proc_double: buf << std::fixed; buf << "{\"double\":" << value << "}";
instead should this
case teck::proc_double: buf << "{\"double\":" << std::fixed << value << "}";
so function can simplified for better readability too
template<typename u> void process(u& buf, dataoption holder) const { if (holder == dataoption::types) { buf << "{\""; switch (type_) { case teck::proc_int: buf << "int"; break; case teck::proc_long: buf << "long"; break; case teck::proc_float: buf << "float"; break; case teck::proc_double: buf << "double"; break; default: buf << type_; } buf << "\":" << std::fixed << value << "}"; } }
Comments
Post a Comment