2

When save NSDecimalNumber in EditViewController, in DisplayTableViewController value appear NaN

Please need advice to solve this issue. Thanks

My code:

- (IBAction)saveChanges:(id)sender {
    editGift.nameOfGift = nameTextField.text;
    editGift.priceOfGift = [NSDecimalNumber decimalNumberWithString:priceTextField.text];

    AppDelegate *myApp = (AppDelegate *) [[UIApplication sharedApplication]delegate];
    [myApp saveContext];
    [self.delegate editGiftViewControllerDidSave:self];
}

Please watch video explanation

Input value:

currencyFormatter = [[NSNumberFormatter alloc]init]; 

[currencyFormatter setNumberStyle:NSNumberFormatterCurrencyStyle];

 personGift.priceOfGift = [NSDecimalNumber decimalNumberWithString:priceTextField.text];
Nikita Pestrov
  • 5,876
  • 4
  • 31
  • 66
German
  • 137
  • 17
  • What's the input value, i.e. what is `priceTextField.text`? –  Dec 19 '12 at 13:33
  • input value: currencyFormatter = [[NSNumberFormatter alloc]init]; [currencyFormatter setNumberStyle:NSNumberFormatterCurrencyStyle]; personGift.priceOfGift = [NSDecimalNumber decimalNumberWithString:priceTextField.text]; – German Dec 19 '12 at 13:35
  • ur input value is not formatted to the NSDecimalNumber thus giving NaN. check the input! Log the textfield value before putting it in the NSDecimalNumber and post it;) – Totumus Maximus Dec 19 '12 at 13:35
  • I meant what is the value of `pricefield.Text`, not what is some other code? –  Dec 19 '12 at 13:38

2 Answers2

4

Your code is unclear about when it applies the currency formatting, but given your video it is very likely that the value of the text field is "$6,000". That is not a number (thus "NaN").

Rob Napier
  • 286,113
  • 34
  • 456
  • 610
  • 1
    I believe what Rob here is saying is what causes your problem. As your getting the text value which includes the '$'-sign. Remove your $-sign from `priceTextField.text` when the user clicks save. Just as a heads up. You could also check for invalid numbers. like '1.2.3.4..00' – Christer Dec 19 '12 at 15:14
  • Thank you very much Rob and Christer, How i can remove $-sign from textfield? – German Dec 19 '12 at 16:33
  • 1
    You already have a currency number formatter handy. Use numberFromString: to convert the text into an `NSNumber`. Ideally, you should use `setGeneratesDecimalNumbers:` so that you get a decimal number out. There used to be a bug that prevented that from working as easily as you'd like, but hopefully by now it's fixed (it's been years). See http://stackoverflow.com/questions/317311/obtaining-an-nsdecimalnumber-from-a-locale-specific-string – Rob Napier Dec 19 '12 at 19:09
2

NSDecimalNumber documentation says:

Besides digits, numericString can include an initial “+” or “–”; a single “E” or “e”, to indicate the exponent of a number in scientific notation; and a single NSDecimalSeparator to divide the fractional from the integral part of the number.

You have to verify that the string in priceTextField.text contains only valid characters - and in valid order. One thing to check especially is whether you have decimal point or decimal comma, depending on user locale. Therefore you might want to consider using:

+ (NSDecimalNumber *)decimalNumberWithString:(NSString *)numericString
locale:(NSDictionary *)locale
Christer
  • 1,651
  • 1
  • 17
  • 34
JOM
  • 8,139
  • 6
  • 78
  • 111