Search code examples
objective-cnsnumbernsnumberformatter

NSNumber being returned as null when formatted using NSNumberFormatterCurrencyStyle


I have a text field that I want to take in values as an NSNumber, but when the data goes into a table view it is returned as null. Here's the code I have:

NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
[numberFormatter setNumberStyle:NSNumberFormatterCurrencyStyle];
NSNumber *numberPrice = [numberFormatter numberFromString: self.itemPriceTextField.text];
self.listItem.itemPrice = numberPrice;
self.listItem.itemDesc = [NSString stringWithFormat:@"%@", numberPrice];

There are no currency symbols in the text field just input from the user with regular whole numbers (ex: 12, 52, or 35)

Any help is appreciated, thanks!


Solution

  • If you use the number style NSNumberFormatterCurrencyStyle then your input is expected to have a currency symbol. Try using NSNumberFormatterDecimalStyle for numbers without the currency symbol .