JH95
JH95

Reputation: 489

Keep leading zeros when converting NSString to Integer

Is there a way to keep the leading zeros when converting a string to an integer. For example say the string was "01" is there a way i could store it as an integer value of 01?

- (int) getNextHand{

    int temp = [[numbersArray objectAtIndex:cardsDelt] intValue]; 
    NSLog(@"Card %i: %i", cardsDelt, temp);
    cardsDelt++;
    return temp;
}

My numbersArray contains 4 leading zero numbers they are: "00" "01" "02" "03"

Upvotes: 2

Views: 1343

Answers (3)

John Heaton
John Heaton

Reputation: 300

If you want to log with leading zeroes use something like %02d

Upvotes: 5

Omar Abdelhafith
Omar Abdelhafith

Reputation: 21221

Nope integers are numbers and 01 will be automatically be converted to 1

Upvotes: 2

Oliver Charlesworth
Oliver Charlesworth

Reputation: 272467

01 and 1 are two representations of the same integer value. If the leading digits contain information, then you have a string, not an integer.

Upvotes: 1

Related Questions