2

Well, I have the following code:

{
    int alfa = CC_RADIANS_TO_DEGREES(atan2(normalized.y, -normalized.x)) + 180;
    NSString * counting = [NSString stringWithFormat:@"fdfg%d.png", alfa];
}

int alfa brings a number between 0 and 360.

What I am trying to achieve is that counting string value is equal to a image file, i have 360 image files, all with this format "fdfg0000.png" "fdfg0024.png" "fdfg0360.png", increasing from 0 to 360, and so on...

the %d operator is not reading the value, because all the zeros leading the file name are already outputted by the program that creates them, without a chance to modify them, I have renamed 360 files manually to this format : "fdfg1.png" "fdfg24.png" "fdfg340.png", and thats the format that %d reads properly.

So my question is, is there anyway to read all the four zeros that the original file includes in a operator function?

Or does anyone know a way to rename 360 files automatically?

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
Juan Boero
  • 6,281
  • 1
  • 44
  • 62

2 Answers2

5

Yes, you can do it: replace %d with %04d to insert leading zeros and make the total number of digits equal 4:

NSString * counting =[NSString stringWithFormat:@"fdfg%04d.png", alfa];
//                                                     ^^
//                                                     ||
//                                  Pad with zeros-----+|
//                          Total number of digits------+

For more information on the format specifiers, read documentation of printf: format specifiers of stringWithFormat: are for the most part compatible with it.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
1

so my question is, is there anyway to read all the four zeros that the original file includes in a operator function?

Use %04d as the format specifier to pad the number to 4 places with leading zeros:

NSString * counting =[NSString stringWithFormat:@"fdfg%04d.png", alfa];

The format specifiers used by NSString (and NSLog, etc.) are pretty much the same set of specifiers used in C with functions like printf() and scanf(), so there's plenty of advice on the net about how to use them to achieve the desired results. The main difference is that Objective-C uses %@ as the format specifier for an Objective-C object.

Caleb
  • 124,013
  • 19
  • 183
  • 272