You need to change it to:
while( toFormat.find(".")!=string::npos // !=string::npos is important!!!
&& toFormat.substr( toFormat.length() - 1, 1) == "0"
|| toFormat.substr( toFormat.length() - 1, 1) == "." )
{
toFormat.pop_back();
}
The key here is to add !=string::npos
. When not found, std::basic_string::find()
will return std::basic_string::npos
, which is not equal to false
(not what you expect).
static const size_type npos = -1;