views:

212

answers:

1

Hi all,

I have a UITextField in my app that accepts decimal amounts for prices (i.e., 123.45). This field is stored in Core Data as an integer.

The code I have that doesn't work is:

[self.managedObject setValue:[textField floatValue]*100 forKey:@"price"]

And I can't quite figure out how to make this work. Any ideas?

Thanks!

+1  A: 
  1. the answer is [[self managedObject] setValue:[NSNumber numberWithInteger:([textField floatValue]*100)] forKey:@"price"]

  2. You should be using NSDecimalNumber for currency as it handles decimals without floating point issues.

Marcus S. Zarra
And use "Decimal" as the attribute type in your data model.
gerry3