How to make a number displayed in a length of digits(eg: 989 as 000989)

Discussion in 'Mac Programming' started by smokyonion, Apr 15, 2010.

  1. smokyonion macrumors newbie

    Dec 13, 2009
    Hi, guys.
    I am learning Cocoa right now, and
    I am working on a small Cocoa project try to learn how to use NSNumberFormatter,

    This project is a simple screen capture program and I want it has the ability to auto save the captured image file.
    I create three NSTextField for users
    one is Filename prefix
    another one is digit,
    and the last is the length of the digit.

    So when users capture the screenshot my program should automatically save file with the naming rule.
    For Example: I have a fifth image captured. and the digit length is 4.
    The file name will be: "FileNamePrefix-0005.jpg"
    And then auto increment one for next image.

    How can I make the digit in the fixed length.
    I have looked up NSNumberFormatter and try to find something can help me on this, however it seems not for this kind of usage.

    Any reply will be highly appreciated, thank you so much.
  2. GorillaPaws macrumors 6502a


    Oct 26, 2003
    Richmond, VA
  3. lee1210 macrumors 68040


    Jan 10, 2005
    Dallas, TX
    NSString *myFormat, *myFilename, *prefixString = @"myFilePrefix";
    NSInteger numWidth = 7,imageNumber = 5;
    myFormat = [NSString stringWithFormat:@"%%s%%0%lldlld.jpg",(long)numWidth];
    myFilename = [NSString stringWithFormat:myFormat,prefixString,(long)imageNumber];
    Something like this should do it. I'm on my phone, so this hasn't been compiled or tested.

  4. smokyonion thread starter macrumors newbie

    Dec 13, 2009
    Dear GorillaPaws and lee1210:

    Thank you so much for your precious help.


    your example code works great for me.
    There is only one character has to be changed.

    Change from %%s to %%@,


    myFormat = [NSString stringWithFormat:mad:"%%s%%0%lldlld.jpg",(long)numWidth];


    myFormat = [NSString stringWithFormat:mad:"%%@%%0%lldlld.jpg",(long)numWidth];

    I am curious about the format you given.
    I don't know I can use stringFormat like this format before.
    (After I NSLog myFormat, I got it. )
    Thank you all. You rock~
  5. lee1210 macrumors 68040


    Jan 10, 2005
    Dallas, TX
    Ah, oops. In my sleepiness I forgot NSString needs %@, I was thinking of char * c-style strings, even though I used NSString in the code.

    If you log what myFormat is, you should see a pretty normal looking format string. The reason for all of the %s when building it is that we need the % to show up in our string, but % is special. It is used for format specifiers, so you have to double it to get a single % in your resulting string.


    edit: again, due to sleepiness I missed that you had looked and figured out what was going on.
  6. gnasher729 macrumors P6


    Nov 25, 2005
    Get an old C book out, look at printf and sprintf and formatting codes and figure out what format turns the number 5 into the string 0005. That's easy, it is all well documented.

    Then find an NSString method that does basically the same as sprintf does.
  7. smokyonion thread starter macrumors newbie

    Dec 13, 2009
    Thank you, gnasher729.

    I have looked up the C book.

    That really helps for me to understand what the format is describing and how to use it.

    Thank you all~

Share This Page