Ready to Level-Up?

This just in! If your rusty iOS skills could use some refining, or if you're just starting out and you want to level-up fast, consider signing up for our upcoming iOS Screencasts

New from iOS Rocket Surgery Swift Video Tutorials: The rapidly evolving Apple-iOS ecosystem has just been thrown into tumult with the recent introduction of the Swift Programming Language. Are you ready to finally try iOS development? Are you needing to switch from Objective-C to Swift? Sign up today for our free video courses!
(By the dear folks at Swift Tutorial Videos, a sister company)

Tuesday, November 5, 2013

A Few iOS Number Formatting Examples

So, you'd like to format numbers in iOS with the NSNumberFormatter, and you need some examples, right?

Here's a few to get you started:


NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
NSString *valString = @"12345.6789";

// $12345.68
[numberFormatter setPositiveFormat:@"$#.00"];
NSString *val1 = [numberFormatter stringFromNumber:[NSNumber numberWithFloat:[valString floatValue]]];
NSLog(@"%@", val1);

// 12,345.68
[numberFormatter setPositiveFormat:@"#,###.##"];
NSString *val2 = [numberFormatter stringFromNumber:[NSNumber numberWithFloat:[valString floatValue]]];
NSLog(@"%@", val2);

// 00012345.68
[numberFormatter setPositiveFormat:@"00000000.##"];
NSString *val3 = [numberFormatter stringFromNumber:[NSNumber numberWithFloat:[valString floatValue]]];
NSLog(@"%@", val3);


But, iOS follows the unicode spec for number formatting, so you can see the full documentation here, however, documentation like this needs a lot of salt to make it digestible:
http://www.unicode.org/reports/tr35/tr35-10.html#Number_Format_Patterns



Read More

About Me

Easy NSDateFormatter Tool

Save yourself some time in formatting your NSDates to NSStrings, and use the Blind NSDate app, which you can download from iTunes. There's also a website where you can format your NSDates: http://www.blindnsdate.com

Popular Posts

Designed By Seo Blogger Templates