Click here to Skip to main content
15,867,453 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello peeps.

I just want to take an integer and return a string with the formatting to include commas.

I don't care about locale, I just want it to include commas.

I am using C++ 11 and the code will run on an iDevice.

I have googled and tried umpteen solutions I found, but some don't work (specifically those with locales) and some don't even compile, others just use std output rather than strings.

It should be simples, surely?

(I am new to C++ BTW so be gentle !)

And please try to lay off the "what have you tried" "show us your code" etc. comments - there's little point me showing you page after page of code that doesn't work.

I'd be happy with something that just loops through a string, concatenating an extra comma into and output string every three characters, frankly - even that seemed to cause an exception when I tried it - but that's probably because I don't know how to iterate over a std::string properly!
Posted

1 solution

Start by iterating over the string: that's easy! std::string has an operator[] overload, so you can access each character one-by-one:
C++
std::string myStr = "Hello!";
for(unsigned int i = 0; i < myStr.length(); i++)
    {
    char c = myStr[i]; //this is your character
    cout << c;
    }

So...adding a comma after each three characters? Not complex, really:
C++
int commaAt = 3;
std::string myStr = "Hello there!";
for(unsigned int i = 0; i < myStr.length(); i++)
    {
    if (commaAt == 0)
        {
        cout << ',';
        commaAt = 3;
        }
    char c = myStr[i]; //this is your character
    cout << c;
    commaAt--;
    }


"if i wanted to return a string rather than cout-ing how can i do that?
It was doing pretty much what you show, but trying to concat the chars to another string where I started to get exceptions..."


That's also pretty easy, when you think about what you are doing:
C++
int commaAt = 3;
std::string myStr = "Hello there!";
std::string output = "";
for(unsigned int i = 0; i < myStr.length(); i++)
    {
    if (commaAt == 0)
        {
        output += ',';
        commaAt = 3;
        }
    commaAt--;
    char c = myStr[i]; //this is your character
    output += c;
    }
 
Share this answer
 
v2
Comments
_Maxxx_ 5-Apr-14 4:29am    
thanks griff - i'll never mention sheep again.
if i wanted to return a string rather than cout-ing how can i do that?
It was doing pretty much what you show, but trying to concat the chars to another string where I started to get exceptions...
OriginalGriff 5-Apr-14 4:43am    
:laugh:
Don't make promises you can't keep!
Answer updated.
_Maxxx_ 5-Apr-14 7:07am    
copied in your code and it works. Sort of!
Obviously your version returns 12345 as 123,45 whereas I wanted 12,345.
so i changed it to loop from length-1 to <0
and output += s[i] changes to output = s[i] + output (and same change to adding the comma

And it hung!

Oops.
I forgot to change to an unsigned int!

cheers Griff!
OriginalGriff 5-Apr-14 7:15am    
Right...what you actually want to do is work from the other end - which actually makes things easier, because you can convert the integer (using % to get the digit, and divide to set up for the next) and generate the output at the same time: you don't need to generate an intermediate string and then process that.
Can you do that on your own, or do you need a hint?
_Maxxx_ 5-Apr-14 7:25am    
I've currently got a formatString method that I'm using to convert the int to a string, (e.g. s = formatString("%d",12345); then using a modified version of your solution (as of about 3 minutes ago) to convert that to put in the commas.
The formatString method uses vsprintf to format - I don't know if that is more a more efficient way of doing it that what you suggest - I'm wary of putting mod and % because this is being used in a 60fps game loop, so efficiency is important...
Although, I might give it a go just to prove I'm not as thick as I was feeling when I couldn't do this earlier on!

Too used to the niceties of C# etc. where you don't have to write all the damn plumbing yourself!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900