Y
yogi_bear_79
I am passing a whole number integer to this function, it converts the
integer to a string, then I want to add commas so a number like
1234567 shows as 1,234,567. I am a little stuck, I belive the loop is
reading the string from right to left which led me to try decrementing
the loop, but a number like 12345 shows up like 123,45. Or is there a
better way all together to achive ths.
string toStr(int &i)
{
std::string s;
std::stringstream out;
out << i;
s = out.str();
for(size_t x = s.size(); x > 0; x--){
if(x != s.size() && x%3 == 0)
s.insert(x,",");
}
return s;
}
integer to a string, then I want to add commas so a number like
1234567 shows as 1,234,567. I am a little stuck, I belive the loop is
reading the string from right to left which led me to try decrementing
the loop, but a number like 12345 shows up like 123,45. Or is there a
better way all together to achive ths.
string toStr(int &i)
{
std::string s;
std::stringstream out;
out << i;
s = out.str();
for(size_t x = s.size(); x > 0; x--){
if(x != s.size() && x%3 == 0)
s.insert(x,",");
}
return s;
}