The NSNumberFormatter Class

The NSNumberFormatter class provides a simple means to format NSNumber objects. The class includes many pre-defined number format styles you can use and the table below list some of them.

nsnumberformatter_class

To practice source code presented in this lesson, you should download and unzip this file.
project_icon_codepractice
Launch the project (CodePractice.xcodeproj) and enter code in the viewController.m file’s buttonTapped method, then click the “Click Me” button to execute the method’s code.

Code below demonstrates how to use the NSNumberFormatter Class to format an NSNumber using the NSNumberFormatterCurrencyStyle format style.

// Create an NSNumber object, box a C floating-point number, then assign it to the NSNumber object
NSNumber *profit = [NSNumber numberWithFloat:135457.89];

// Create an NSNumberFormatter object
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];

// Set the formatter objec's style
[formatter setNumberStyle:NSNumberFormatterCurrencyStyle];

// Format the profit object befor assinging it to a string object
NSString *formattedProfit = [formatter stringFromNumber:profit];

// Display the formatted number string object
NSLog(@"%@",formattedProfit);

Output
$135,457.89