Even if nothing in <iomanip>
can adjust the actual stream, we can at least not depend on the value passed to setprecision()
, like this:
const std::string& t = s.str();
return t.find_first_of("123456789") == t.npos && t[0] == '-' ?
t.substr(1) : t;
If no digits from 1..9 are found, then all digits must be 0.
If, furthermore, it starts with minus, then skip that character.