Formatter in Cocoa

Formatter in Cocoa

Formatters play a key role in creating a great Cocoa, iOS Application. Foundation framework provides three highly useful class “NSFormatter, NSDateFormatter, NSNumberFormatter” which helps achieve almost any formatting requirement.

Mostly there are convenience methods available in these classes that would suffice to any basic requirements and for any specific custom formatting requirements, developers can write their own Custom Formatters.

The user preferred language, locale, currency are important aspects of a Custom Formatter.

In this post we will dive into the nuances of Custom Formatters

NSDateFormatter

NSDateFormatter helps create String representation of NSDate Objects and converts textual representation of dates and times into NSDate Objects

Date Formatter Styles

NSDateFormatter comes with five date and time styles class methods. 

NSDate *date = [NSDate date];
NSString *dateNoStyle = [NSDateFormatter localizedStringFromDate:date dateStyle:NSDateFormatterNoStyle timeStyle:NSDateFormatterNoStyle];
NSLog(@"NSDateFormatterNoStyle = %@",dateNoStyle);
Output: NSDateFormatterNoStyle = 

NSString *dateShortStyle =[NSDateFormatter localizedStringFromDate:date dateStyle:NSDateFormatterShortStyle timeStyle:NSDateFormatterShortStyle];
NSLog(@"NSDateFormatterShortStyle = %@",dateShortStyle);
NSDateFormatterShortStyle = 3/31/14, 11:00 AM

NSString *dateMediumStyle =[NSDateFormatter localizedStringFromDate:date dateStyle:NSDateFormatterMediumStyle timeStyle:NSDateFormatterMediumStyle];
NSLog(@"NSDateFormatterMediumStyle = %@",dateMediumStyle);
NSDateFormatterMediumStyle = Mar 31, 2014, 11:00:03 AM

NSString *dateLongStyle =[NSDateFormatter localizedStringFromDate:date dateStyle:NSDateFormatterLongStyle timeStyle:NSDateFormatterLongStyle];
NSLog(@"NSDateFormatterLongStyle = %@",dateLongStyle);
NSDateFormatterLongStyle = March 31, 2014 at 11:00:03 AM PDT

NSString *dateFullStyle =[NSDateFormatter localizedStringFromDate:date dateStyle:NSDateFormatterFullStyle timeStyle:NSDateFormatterFullStyle];
NSLog(@"NSDateFormatterFullStyle = %@",dateFullStyle);
NSDateFormatterFullStyle = Monday, March 31, 2014 at 11:00:03 AM Pacific Daylight Time

Custom Date Formatter

Following is a simple Custom Date Formatter

NSDate *date = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"dd-MM-yyyy"];
NSString *dateString = [dateFormatter stringFromDate:date];
NSLog(@"Output dateString dd-MM-yyyy = %@",dateString);
Output dateString dd-MM-yyyy = 31-03-2014

[dateFormatter setDateFormat:@"dd-MMM-yyyy"];
dateString = [dateFormatter stringFromDate:date];
NSLog(@"Output dateString dd-MMM-yyyy = %@",dateString);
Output dateString dd-MMM-yyyy = 31-Mar-2014

[dateFormatter setDateFormat:@"MM/dd/yyyy hh:mm a"];
dateString = [dateFormatter stringFromDate:date];
NSLog(@"Output dateString MM/dd/yyyy hh:mm a = %@",dateString);
Output dateString MM/dd/yyyy hh:mm a = 03/31/2014 11:29 AM

Separating the Components of a date using multiple DateFormatter

In the below sample we will  use two custom date formatter to separate the date and time components of a date object

NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"yyyy-MM-dd"];

NSDateFormatter *timeFormat = [[NSDateFormatter alloc] init];
[timeFormat setDateFormat:@"HH:mm:ss"];

NSDate *newDate = [[NSDate alloc] init];
NSString *theDate = [dateFormat stringFromDate:newDate];
NSString *theTime = [timeFormat stringFromDate:newDate];
NSLog(@"theDate is = %@ and the time is = %@", theDate, theTime);
Output: theDate is = 2014-03-31 and the time is = 11:29:15

Detailed Custom Date Formatter

In the below sample code we will create a little more detailed custom date formatter and will tweak the Weekdays, Month symbols

NSDate *date = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateStyle:kCFDateFormatterFullStyle];
[dateFormatter setTimeStyle:kCFDateFormatterFullStyle];
//    NSLog(@"Long Era Symbols are %@",[dateFormatter longEraSymbols]);
//    NSLog(@"timeZone = %@",[dateFormatter timeZone]);

NSString *dateString = [dateFormatter stringFromDate:date];
NSLog(@"dateString before applying the custom format is = \n%@",dateString);

[dateFormatter setMonthSymbols:@[@"Ja",@"Fe",@"Ma",@"Ap",@"Ma",@"Jn",@"Jl",@"Au",@"Se",@"Oc",@"No",@"De"]];
[dateFormatter setPMSymbol:@"Post Noon"];
[dateFormatter setAMSymbol:@"Before Noon"];
[dateFormatter setWeekdaySymbols:@[@"Su",@"Mo",@"Tu",@"We",@"Th",@"Fr",@"Sa"]]; //Starts with Sunday
dateString = [dateFormatter stringFromDate:date];
NSLog(@"dateString after applying the custom format is = \n%@",dateString);

Output: dateString before applying the custom format is = 
Monday, March 31, 2014 at 12:21:12 PM Pacific Daylight Time

Output: dateString after applying the custom format is = 
Mo, Ma 31, 2014 at 12:21:12 Post Noon Pacific Daylight Time

NSNumberFormatter

NSNumberFormatter converts textual representation of Numeric Values to NSNumber Objects.
Ranges (min and max) can also be imposed using a NSNumberFormatter
The two most important methods of a NSNumberFormatter are stringFromNumber and numberFromString

Below sample code is a simple NSNumberFormatter

NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
[numberFormatter setNumberStyle:NSNumberFormatterDecimalStyle];
NSString *formattedNumberString = [numberFormatter stringFromNumber:@1234567.8976];
NSLog(@"formattedNumberString: %@", formattedNumberString);
Output:formattedNumberString: 1,234,567.898    

Following are the style constants that can be used for a particular number representation

    
NSLog(@"1234567.8976 with NSNumberFormatterNoStyle = %@",[NSNumberFormatter localizedStringFromNumber:@1234567.8976 numberStyle:NSNumberFormatterNoStyle]);
Output: 1234567.8976 with NSNumberFormatterNoStyle = 1234568

NSLog(@"1234567.8976 with NSNumberFormatterDecimalStyle  = %@",[NSNumberFormatter localizedStringFromNumber:@1234567.8976 numberStyle:NSNumberFormatterDecimalStyle]);
Output: 1234567.8976 with NSNumberFormatterDecimalStyle  = 1,234,567.898

NSLog(@"1234567.8976 with NSNumberFormatterCurrencyStyle =  %@",[NSNumberFormatter localizedStringFromNumber:@1234567.8976 numberStyle:NSNumberFormatterCurrencyStyle]);
Output: 1234567.8976 with NSNumberFormatterCurrencyStyle =  $1,234,567.90

NSLog(@"1234567.8976 with NSNumberFormatterPercentStyle  = %@",[NSNumberFormatter localizedStringFromNumber:@1234567.8976 numberStyle:NSNumberFormatterPercentStyle]);
Output: 1234567.8976 with NSNumberFormatterPercentStyle  = 123,456,790%

NSLog(@"1234567.8976 with NSNumberFormatterScientificStyle  = %@",[NSNumberFormatter localizedStringFromNumber:@1234567.8976 numberStyle:NSNumberFormatterScientificStyle]);
Output: 1234567.8976 with NSNumberFormatterScientificStyle  = 1.2345678976E6

NSLog(@"1234567.8976 with NSNumberFormatterSpellOutStyle  = %@",[NSNumberFormatter localizedStringFromNumber:@1234567.8976 numberStyle:NSNumberFormatterSpellOutStyle]);
Output: 1234567.8976 with NSNumberFormatterSpellOutStyle  = one million two hundred thirty-four thousand five hundred sixty-seven point eight nine seven six

Custom number formatter

NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
[formatter setNumberStyle:NSNumberFormatterCurrencyStyle];
[formatter setMinimumFractionDigits:(unsigned int)0];
[formatter setMaximumFractionDigits:(unsigned int)2];
[formatter setCurrencySymbol:@"$"];
[formatter setNegativePrefix:@"-$"];
[formatter setNegativeSuffix:@""];
formattedNumberString = [formatter stringFromNumber:@1234567.8976];
NSLog(@"formattedNumberString: %@", formattedNumberString);

Output:formattedNumberString: $1,234,567.9
If negative Prefix and Suffix is not set a negative number such as -1234567.8976 will be represented as ($1,234,567.9) 
If we desire the output to be as -$1,234,567.9 then we have specifically state the negative prefix and suffix

Rounding Modes

The following rounding modes are available to round the number. use setRoundingMode

  • NSNumberFormatterRoundCeiling
  • NSNumberFormatterRoundFloor
  • NSNumberFormatterRoundDown
  • NSNumberFormatterRoundUp
  • NSNumberFormatterRoundHalfEven
  • NSNumberFormatterRoundHalfDown
  • NSNumberFormatterRoundHalfUp
Posted in Cocoa, Objective C Tagged with: , , , ,

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

Recent Posts


Hit Counter provided by technology news