I have a below code in one of the library which is causing some numbers to get shown in the 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 some of the different cases above, "value" is coming in scientific notation. How can I avoid showing scientific notation and instead show full number? I did some research and I can use "std::fixed" but where I should be using this?
std::fixed
works within the same stream, so this won't work in case you're working with a stateless stream
case teck::PROC_DOUBLE:
buf << std::fixed;
buf << "{\"double\":" << value << "}";
Instead it should be like this
case teck::PROC_DOUBLE:
buf << "{\"double\":" << std::fixed << value << "}";
So your function can be simplified like this 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 << "}";
}
}