I am converting an NSNumber
to an NSString
assuming in this example that the selected key in "theindex"
is 1000000
NSNumber *firstNumber = [tempDict objectForKey:@"theindex"];
NSString *convertNumber = [firstNumber stringValue];
Returning the NSString
"1000000"
I would like the string's value to be "1,000,000".
I am not concerned with localization, but understand from other questions that NSNumberFormatter
should be implemented. I am not sure how to accomplish this?
Use an NSNumberFormatter : NSNumberFormatter *f = [[NSNumberFormatter alloc] init]; f. numberStyle = NSNumberFormatterDecimalStyle; NSNumber *myNumber = [f numberFromString:@"42"]; If the string is not a valid number, then myNumber will be nil .
NSNumber is a subclass of NSValue that offers a value as any C scalar (numeric) type. It defines a set of methods specifically for setting and accessing the value as a signed or unsigned char , short int , int , long int , long long int , float , or double or as a BOOL .
Here is an example using a standard formatting style of NSNumberFormatter:
NSNumber *firstNumber = [NSNumber numberWithInt:123456789];
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
[formatter setNumberStyle:NSNumberFormatterDecimalStyle];
NSString *convertNumber = [formatter stringForObjectValue:firstNumber];
NSLog(@"value : %@", convertNumber);
Other examples you can reference at:
http://iosdevelopertips.com/cocoa/formatting-numbers-nsnumberformatter-examples.html
NSNumber *firstNumber = [[NSNumber alloc] initWithInt:1000000] ;
NSNumberFormatter *format=[[NSNumberFormatter alloc] init];
[format setGroupingSize:3];
[format setCurrencyGroupingSeparator:@","];
[format setNumberStyle:NSNumberFormatterDecimalStyle];
NSString *convertNumber = [format stringFromNumber:firstNumber];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With