Hi guys this is my first question and i guess can be easy for you, i want to convert this NSString “42031692.7810” to something like this: $4,2031,692.78, i have the this code:
NSString *element = @"42031692.7810";
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
[formatter setNumberStyle:NSNumberFormatterCurrencyStyle];
[formatter setGeneratesDecimalNumbers:YES];
[formatter setMaximumFractionDigits:2];
NSNumber *temp = [formatter numberFromString:element];
NSLog(@"formatter%@",[temp stringValue]);
but the output is something like this:
2012-04-19 14:43:42.467 HelloWorld[5663:f803] formatter(null)
How can i fixed this??? Hope you can help me!!
Regards!
You have the string
@"42031692.7810", and you have anNSNumberFormatterset to accept things that are styled as currency.@"42031692.7810"is not styled as currency. Thus it is returningnil.You have to set the number formatter to accept decimal input, turn the string into an
NSNumber, then set the formatter style to the “Currency” style, and then turn theNSNumberback into anNSString.