Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > C++ > Help add commas...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 1 of 19 Topic 44259 of 47561
Post > Topic >>

Help add commas to int on console output

by yogi_bear_79 <yogi_bear_79@[EMAIL PROTECTED] > Mar 31, 2008 at 06:15 PM

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;
}
 




 19 Posts in Topic:
Help add commas to int on console output
yogi_bear_79 <yogi_bea  2008-03-31 18:15:26 
Re: Help add commas to int on console output
Kai-Uwe Bux <jkherciue  2008-03-31 21:52:36 
Re: Help add commas to int on console output
Martin York <Martin.Yo  2008-03-31 20:14:51 
Re: Help add commas to int on console output
"Jim Langston"   2008-03-31 20:25:01 
Re: Help add commas to int on console output
James Kanze <james.kan  2008-04-01 01:42:13 
Re: Help add commas to int on console output
James Kanze <james.kan  2008-04-01 01:53:57 
Re: Help add commas to int on console output
Jerry Coffin <jerry.co  2008-04-10 21:48:00 
Re: Help add commas to int on console output
James Kanze <james.kan  2008-04-11 02:32:41 
Re: Help add commas to int on console output
Jerry Coffin <jcoffin@  2008-04-11 11:25:08 
Re: Help add commas to int on console output
peter koch <peter.koch  2008-04-11 04:19:33 
Re: Help add commas to int on console output
James Kanze <james.kan  2008-04-12 00:58:18 
Re: Help add commas to int on console output
James Kanze <james.kan  2008-04-12 01:02:23 
Re: Help add commas to int on console output
Jerry Coffin <jcoffin@  2008-04-12 19:52:24 
Re: Help add commas to int on console output
peter koch <peter.koch  2008-04-12 06:11:58 
Re: Help add commas to int on console output
James Kanze <james.kan  2008-04-13 02:37:26 
Re: Help add commas to int on console output
James Kanze <james.kan  2008-04-13 02:45:39 
Re: Help add commas to int on console output
Jerry Coffin <jcoffin@  2008-04-13 10:37:26 
Re: Help add commas to int on console output
Ben Bacarisse <ben.use  2008-04-13 13:24:47 
Re: Help add commas to int on console output
Jerry Coffin <jcoffin@  2008-04-13 09:34:23 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Sun Sep 7 3:44:09 CDT 2008.