10

I have an 8 digit integer which I would like to print formatted like this:

XXX-XX-XXX

I would like to use a function that takes an int and returns a string.

What's a good way to do this?

0

10 Answers 10

12

This is how I'd do it, personally. Might not be the fastest way of solving the problem, and definitely not as reusable as egrunin's function, but it strikes me as both clean and easy to understand. I'll throw it in the ring as an alternative to the mathier and loopier solutions.

#include <sstream>
#include <string>
#include <iomanip>

std::string format(long num) {
  std::ostringstream oss;
  oss << std::setfill('0') << std::setw(8) << num;
  return oss.str().insert(3, "-").insert(6, "-");
};
1
  • definitely the easiest to understand (that is read) of all the answers.
    – Inverse
    May 12, 2010 at 5:46
6

Tested this, it works.

The format parameter here is "XXX-XX-XXX", but it only looks at (and skips over) the dashes.

std::string foo(char *format, long num)
{
    std::string s(format);

    if (num < 0) { return "Input must be positive"; }

    for (int nPos = s.length() - 1; nPos >= 0; --nPos)
    {
        if (s.at(nPos) == '-') continue;

        s.at(nPos) = '0' + (num % 10);
        num = num / 10;
    }

    if (num > 0) { return "Input too large for format string"; }

    return s;
}

Usage:

int main()
{
    printf(foo("###-##-###", 12345678).c_str());

    return 0;
}
6
  • 3
    But what does cout << foo("###-##", 12345) << endl do? If the function is only going to work with one particular length of format string, you should assert that. IMO you should assert range conditions rather than failing silently also.
    – clstrfsck
    May 12, 2010 at 3:16
  • 2
    Not only YAGNI, but duplicating something that's already in the standard library. Almost enough to qualify for a -1... May 12, 2010 at 3:18
  • for the sake of abstraction, i would use if (s.at(nPos) != '#') continue; instead of if (s.at(nPos) == '-') continue;
    – goldPseudo
    May 12, 2010 at 3:22
  • 2
    Hey guys, it's an example which solves the exact problem and suggests ways to take it further...I mean, you don't leave "really an error" in production code, do you? Sheesh, tough crowd. I'm a big believer in YAGNI myself.
    – egrunin
    May 12, 2010 at 7:18
  • Yes, it's a tough crowd, but you also got a few up votes to go with the criticism. If it's any consolation, I'm kind of laughing at myself right now for spending FAR longer on this than I EVER would have if I had to solve this problem in anger.
    – clstrfsck
    May 12, 2010 at 12:49
5

Here's a bit different way that tries to work with the standard library and get it to do most of the real work:

#include <locale>

template <class T>
struct formatter : std::numpunct<T> {
protected:
    T do_thousands_sep() const { return T('-'); }
    std::basic_string<T> do_grouping() const {
        return std::basic_string<T>("\3\2\3");
    }
};

#ifdef TEST

#include <iostream>

int main() {
    std::locale fmt(std::locale::classic(), new formatter<char>);   
    std::cout.imbue(fmt);

    std::cout << 12345678 << std::endl;
    return 0;
}

#endif

To return a string, just write to a stringstream, and return its .str().

This may be overkill if you only want to print out one number that way, but if you want to do this sort of thing in more than one place (or, especially, if you want to format all numbers going to a particular stream that way) it becomes more reasonable.

1
  • 3
    I can't really see too many people solving this particular problem in this way for real, but +1 for the most awesome answer.
    – clstrfsck
    May 12, 2010 at 12:54
4

Here's a complete program that shows how I'd do it:

#include <iostream>
#include <iomanip>
#include <sstream>

std::string formatInt (unsigned int i) {
    std::stringstream s;
    s << std::setfill('0') << std::setw(3) << ((i % 100000000) / 100000) << '-'
      << std::setfill('0') << std::setw(2) << ((i % 100000) / 1000) << '-'
      << std::setfill('0') << std::setw(3) << (i % 1000);
    return s.str();
}

int main (int argc, char *argv[]) {
    if (argc > 1)
        std::cout << formatInt (atoi (argv[1])) << std::endl;
    else
        std::cout << "Provide an argument, ya goose!" << std::endl;
    return 0;
}

Running this with certain inputs gives:

Input        Output
--------     ----------
12345678     123-45-678
0            000-00-000
7012         000-07-012
10101010     101-01-010
123456789    234-56-789
-7           949-67-289

Those last two show the importance of testing. If you want different behaviour, you'll need to modify the code. I generally opt for silent enforcement of rules if the caller can't be bothered (or is too stupid) to follow them but apparently some people like to use the principle of least astonishment and raise an exception :-)

3

You can use the std::ostringstream class to convert the number to a string. Then you can use the string of digits and print them using whatever formatting you want, as in the following code:

std::ostringstream oss;
oss << std::setfill('0') << std::setw(8) << number;
std::string str = oss.str();
if ( str.length() != 8 ){
    // some form of handling
}else{
    // print digits formatted as desired
}
1
int your_number = 12345678;

std::cout << (your_number/10000000) % 10 << (your_number/1000000) % 10 << (your_number/100000) %10 << "-" << (your_number/10000) %10 << (your_number/1000) %10 << "-" << (your_number/100) %10 << (your_number/10) %10 << (your_number) %10;

http://www.ideone.com/17eRv

Its not a function, but its a general method for parsing an int number by number.

1
#include <iostream>
#include <string>

using namespace std;

template<class Int, class Bi>
void format(Int n, Bi first, Bi last)
{
    if( first == last ) return;
    while( n != 0 ) {
        Int t(n % 10);
        n /= 10;
        while( *--last != 'X' && last != first);
        *last = t + '0';
    }
}

int main(int argc, char* argv[])
{
    int i = 23462345;
    string s("XXX-XX-XXX");
    format(i, s.begin(), s.end());
    cout << s << endl;
    return 0;
}
2
  • This algorithm will work with any integer so long as the format string has number of X characters equal to the length of the integer. For instance, in the case of an integer with 8 digit the format string must have 8 X characters. May 12, 2010 at 3:13
  • It might therefore be better to terminate based on first == last instead of n == 0.
    – Ben Voigt
    Dec 26, 2012 at 15:41
1

How's this?

std::string format(int x)
{
    std::stringstream ss

    ss.fill('0');
    ss.width(3);
    ss << (x / 10000);

    ss.width(1);
    ss << "-";

    ss.width(2);
    ss << (x / 1000) % 100;

    ss.width(1);
    ss << "-";

    ss.width(3);
    ss << x % 1000;

    return ss.str();
}

Edit 1: I see strstream is deprecated and replaced with stringstream.

Edit 2: Fixed issue of missing leading 0's. I know, it's ugly.

0
0

Obviously a char * and not a string, but you get the idea. You'll need to free the output once you're done, and you should probably add error checking, but this should do it:

char * formatter(int i)
{    
  char *buf = malloc(11*sizeof(char));
  sprintf(buf, "%03d-%02d-%03d", i/100000, (i/1000)%100, i%1000);
  return buf;
}
2
  • 1
    why malloc, we're in C++ here May 12, 2010 at 2:23
  • 1
    Why manual allocation? If anything, make the buffer automatic and not dynamic (sizeof(char) is always 1, make a buffer of 16 just because it's a nice number), sprintf into that, then return a std::string.
    – GManNickG
    May 12, 2010 at 2:29
-1

You don't require malloc or new, just define buf as char buff[11];

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.