I am trying to round in scientific notation but considering the correct rule which is: A) for all decimal numbers finishing by 0, 1, 2, 3, 4 the rounding must be at the low order of magnitude B) for all decimal numbers finishing by 5, 6, 7, 8, 9 the rounding must be up.
I did the following function:
std::string MyClass::doubleToString(double num, int precision) {
std::ostringstream ss;
ss << std::scientific << std::setprecision(precision) << num;
return ss.str();
}
For instance, when I call doubleToString(0.35, 0)
, it returns 3E-1 while it should return 4E-1.
I think it is a very simple question but I cannot figure out why I do not get the correct behavior.