I have the following code:
template<typename _Tp>
std::string string_from_number(_Tp number, int precision = 5)
{
std::ostringstream oss;
oss.width(precision+1);
oss<<number;
return oss.str();
}
However the string length is not 5 as expected, but stays the default.
Replacing oss.width to oss<<std::setw does not work either.
What am I doing wrong?
EDIT:
The problem appears clearly if the number is of double type say 0.123456789.
For controlling the number of digits displayed in a floating point number, the function you want is
precision, notwidth. If you want to be sure you get the number displayed in fixed notation (rather than scientific) also look at the format flagfixed, used with thesetfmember function.