-2

I'm trying to save a month and the format needs to be 2 digits. The problem is the xcode is converting every month below October to 1 digit. How can I format the number so that it will be for example 01 for January?

Edit saving as a nsnumber. This is for an HTTP request call to stripe. The date that gets sent keeps changing from 0X to X for all months before October.

Edit 2 I'm also getting the following warning from where I convert the stripe card to a nsnumber

Implicit conversion loses integer precision:'NSUInteger' (aka 'unsigned long') to 'int'

Edit 3 To clarify, the number 01 keeps getting changed to 1. I want to save it as 01. How do I change the format of the number?

madgrand
  • 121
  • 1
  • 8

1 Answers1

1

You could use either string formatting, or a number formatter. Either way, the output is going to be a string since you can't format the number its self. Here's an example:

NSNumber *number = @(9);

// using a number formatter
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
[formatter setNumberStyle:NSNumberFormatterNoStyle];
[formatter setMinimumIntegerDigits:2];

NSString *numberString = [formatter stringFromNumber:number];
NSLog(@"%@",numberString);

// using string formatters
numberString = [NSString stringWithFormat:@"%02ld",(long)[number integerValue]];
NSLog(@"%@",numberString);
Aaron Wojnowski
  • 6,352
  • 5
  • 29
  • 46