I am a beginner Objective-C programmer and I was building a calculator as my first app. I am using an NSNumberFormatter to format the Calculator’s display. It converts NSNumber to NSString correctly, but doesn’t parse NSString to NSNumber right if they have a comma separator. Here’s the code:
- (IBAction)digitPressed:(NSButton *)sender {
if (userIsEnteringANumber) {
NSNumber *number = [formatter numberFromString:[self.calDisplay.stringValue stringByAppendingString:sender.title]];
self.calDisplay.stringValue = [formatter stringFromNumber:number];
}
else {
self.calDisplay.stringValue = sender.title;
userIsEnteringANumber = YES;
}
}
If calDisplay.stringValue = @“2,569”, then pressing another digit sets number to nil.
Pragmatically I would simply remove the comma before parsing the string into a number then it must work.
Leaving you with