0

Yes you read that right, Im looking for a way of incrementing integer's precision before the decimal point. The reason is irrelevant and would be long to explain. Specifically, I want to increment it to the hundreds, for instance:

  • 1 --> 001
  • 7 --> 007
  • 27 --> 027
  • 358 --> 358

...and so on and so forth.

Im aware there's a way of just programing this, but I presume there's a way of doing this automatically, am I right? Just like the "setprecision()" command, but the way around as you can see. I've looked up for this a long time but all previous questions just regard the precision after the decimal point.

Thanks in advance to anyone who can help me in this weird request.

F.Webber
  • 195
  • 8
  • 5
    Looks like `setfill` and `setw` would work. – chris May 29 '13 at 00:41
  • See http://stackoverflow.com/questions/1714515/how-can-i-pad-an-int-with-leading-zeros-when-using-cout-operator. In your searching, you want to use the terms "zero padding". – plasma May 29 '13 at 00:43
  • Oh, sorry for making a duplicate question guys, seems like I didn't use the proper terms when searching. Anyway, thanks for the tip, that certainly makes it much easier than programing it. – F.Webber May 29 '13 at 13:24

1 Answers1

1

I wrote something like this a short while back.

I'm not saying this is perfect but you get the idea.

Use recursion:

int someNumber = 235;
int number = someNumber;
int minDigits = 5;
int actualDigits = 0;

string intString = "";

int digitCount = 0;

while (number > 0)
{
    number /= 10;
    actualDigits ++;
}

intString = itoa( someNumber );

while (actualDigits < minDigits)
{
    intString = string("0") + intString;
    actualDigits++;
}

now intString will be "00235"

Sellorio
  • 1,806
  • 1
  • 16
  • 32
  • Thanks for the program, thats more or less what I was thinking with programing, although what I was looking for was setfill and setw. – F.Webber May 29 '13 at 13:30