iOS 4+; when I pass a nil value to my NSNumberFormatter I want the nil symbol that was set (NSString *) in return. It works for the 'zero' symbol, but not for the nil symbol. I've tried many different configuration settings like behavior, grouping, etc.
fmtr = [[NSNumberFormatter alloc] init];
[fmtr setNumberStyle:NSNumberFormatterDecimalStyle];
[fmtr setFormatterBehavior:NSNumberFormatterBehaviorDefault];
[fmtr setUsesGroupingSeparator:NO];
[fmtr setNilSymbol:@"###"];
[fmtr setZeroSymbol:@"0000"];
NSLog(@"[fmtr nilSymbol]=>>%@<<", [fmtr nilSymbol]);
NSLog(@"[fmtr stringFromNumber:nil]=>>%@<< this should be '###", [fmtr stringFromNumber:nil] );
NSLog(@"[fmtr stringFromNumber:0]=>>%@<<", [fmtr stringFromNumber:[NSNumber numberWithInt:0]] );
NSLog(@"[fmtr stringFromNumber:null]=>>%@<<", [fmtr stringFromNumber:NULL] );
NSLog(@"[fmtr numberFromString:nil]=%@", [fmtr numberFromString:nil] );
NSLog(@"[fmtr numberFromString:@\"0\"]=%@", [fmtr numberFromString:@"0"] );
NSLog(@"[fmtr numberFromString:NULL]=%@", [fmtr numberFromString:NULL] );
Here is the output of the test:
[fmtr nilSymbol]=>>###<<
[fmtr stringFromNumber:nil]=>>(null)<< this should be '###
[fmtr stringFromNumber:0]=>>0000<<
[fmtr stringFromNumber:null]=>>(null)<<
[fmtr numberFromString:nil]=(null)
[fmtr numberFromString:@"0"]=0
[fmtr numberFromString:NULL]=(null)
I'm not sure exactly why it doesn't work when using numberFromString, but I tested this myself, and it does work if you use "stringForObjectValue" instead. (replace all "stringFromNumber" with "stringForObjectValue")
Edit: Found the answer
On this blog: http://www.nsformatter.com/blog/2010/6/9/nsnumberformatter.html
it says:
- (NSString *)stringFromNumber:(NSNumber *)number;
This is a convenience method. This method returns nil, if number is nil, otherwise it calls -stringForObjectValue:. Therefore it behaves like -stringForObjectValue:, except it never returns the -nilSymbol
It seems to be the purpose of the method (not to use the nilSymbol)
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