Leading zeros

Discussion in 'Mac Programming' started by cpuin, Oct 11, 2013.

  1. cpuin macrumors member

    Joined:
    Feb 3, 2013
    #1
    I wan to make an invoice app.
    The number of each document should be like this:

    0000000001
    0000000023

    What is written in Stack-overflow doesn't work for me:

    Code:
    NSString *myString = [[NSString alloc] initWithFormat:@"%05d", 5];
            [_documentNumber setStringValue:myString];
    
     
  2. devilofspades, Oct 11, 2013
    Last edited: Oct 11, 2013

    devilofspades macrumors member

    Joined:
    Jul 20, 2011
    #2
    where are the document numbers being generated? better question is, what is keeping track of these document numbers? are they sequential?

    to create a string you just simply need to do

    Code:
     NSString *myString = @"0000000001"; 
    you could also do something like

    Code:
     
    int i = 23;
    NSString *myString = [NSString stringWithFormat:@"00000000%i", i];
    
    typically you don't need to "alloc" a string because it does it for you under the hood. my guess that code you posted should be

    Code:
    NSString *myString = [NSString stringWithFormat:@"%05d", 5];
    
    the unfortunate thing is there are several ways to do what you are asking, and the best answer might require a bit more insight to how you are generating your document numbers.
     
  3. robvas macrumors 68020

    Joined:
    Mar 29, 2009
    Location:
    USA
    #3
    Storing an invoice number as a string sounds like a headache waiting to happen. Why not make it a number then just do the formatting when you display the number?

    You just complicated things like sorting and searching.
     
  4. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #4
    Please explain exactly how it doesn't work.
    1. Describe what you expected to happen.
    2. Describe what actually happened.

    For example, suppose it produces a 5-digit zero-filled value, and you expected it to produce a 10-digit zero-padded value. You need to tell us what it actually produces, and why that isn't what you expected.

    From what you've posted, you actually want to produce a 10-digit zero-padded string. That's what I counted as the number of digits in "0000000023".

    Now, given that "%05d" will only produce a 5-digit zero-padded string, I see an immediate mismatch between what was specified ("0000000023") and what the code should actually produce. So if the code produces a 5-digit zero-padded string, then in my view, it's behaving correctly. You can say it doesn't work, but that would be because you told it the wrong thing to do ("%05d"), not a problem with the initWithFormat: method. If you us a format of "%010d", what happens?

    I will also point out that if the number being converted to a string is negative, then one character in the resulting string will be a minus sign, so there will be one fewer zeros of padding.
     
  5. cpuin thread starter macrumors member

    Joined:
    Feb 3, 2013
    #5


    What is happening is that no 0 appears in front.
     
  6. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #6
    1. Post your evidence.

    This means the code that calls NSLog, or whatever it does that tells you there aren't any leading zeros. If there's a method or property that the NSLog depends on, then post its code, too.

    It also means the actual NSLog output text.


    2. Make a test case.

    Write a simplified test program, then run it, and post both the complete test code and its output.

    Here's my test case:
    fmtTest.m
    Code:
    #import <Foundation/Foundation.h>
    
    int main(int arcgc, char *argv[])
    {
    	NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    
    
    	NSString * str1 = [[NSString alloc] initWithFormat:@"%010d", 23];
    	NSString * str2 = [NSString stringWithFormat:@"%010d", 42];
    
    	NSLog( @"str1: %@", str1 );
    	NSLog( @"str2: %@", str2 );
    
    	[str1 release];
    	
    	[pool drain];
    	return 0;
    }
    
    Output:
    Code:
    2013-10-12 09:04:00.294 a.out[20190] str1: 0000000023
    2013-10-12 09:04:00.294 a.out[20190] str2: 0000000042
    
    Note that it uses both the initWithFormat: and stringWithFormat: methods. Also note that it uses the "%010d" format to produce the 10-digit format, not a 5-digit format.
     
  7. cpuin thread starter macrumors member

    Joined:
    Feb 3, 2013
    #7

    Thank you very much for your time.

    It's what i have tried:

    Code:
    NSString * str2 = [NSString stringWithFormat:@"%010d", 42];
            NSLog( @"str2: %@", str2 );
            [_documentNumber setStringValue:str2];
    
    Where _documentNumber is instance of NSTextField

    The log returns 0000000042, but the label displays 42
     
  8. Brian Y macrumors 68040

    Joined:
    Oct 21, 2012
    #8
    Honestly, do yourself a favour and just store the invoice ID. Add the leading zeros in the UI.

    It'll save you a lot of headaches.
     
  9. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #9
    Since the NSString is correct, as shown by NSLog, then logically the problem can't be with stringWithFormat:.

    That leaves the NSTextField. Or the NSTextFieldCell it contains. I suggest looking into those more closely, including their superclasses. Also be sure to look at the NSFormatter that can be assigned to an NSCell.

    You may need to learn to use the debugger, too, such as setting breakpoints, stepping, etc.
     

Share This Page