title: Casting calculated value to decimal and saving to Core Data
body: <p>I'm new to iPhone dev, so this probably seems like a really silly question, but how can I get a calculated value saved to core data?</p>
  
  <p><strong>Background</strong>
  This program simply calculates a value from what the user inputs into text fields. I have their data types as NSStrings and when saved to core data, everything works fine. 
  I'm casting the NSStrings to floats and perform the appropriate mathematical operations in order to come up with the final calculated value. This final value is not output to a text field, but is saved in core data. I keep getting these two errors on the same line: *"Passing 'double' to parameter of incompatible type 'NSString <em>'</em> and "Assigning to 'NSString *' from incompatible type 'double.' (line 10 is the one with these messages)</p>
  
  <pre><code>1     float dollar1 = [self.dataModel.price          floatValue];
  2     float P1      = [self.dataModel.percentage     floatValue];
  3     float dollar2 = [self.dataModel.price          floatValue];
  4     float P2      = [self.dataModel.percentage2    floatValue];
  
  5     float totalPrice = dollar1 + dollar2;
  6     float totalPercentage = (P1 + P2) / 100;
  
  7     double finalPrice = round(totalPrice / totalPercentage);
  
  8     //Set final price to save in core data
  9     NSLog(@"setting finalPrice to core data slot");
  10    dataModel.finalPrice = finalPrice;
  </code></pre>
  
  <p>*Right before I posted this code, I tried changing line 7 to</p>
  
  <p><code>NSString finalPrice = totalPrice / totalPercentage;</code></p>
  
  <p>which caused the above mentioned errors on line 10 to go away, but caused an error on line 7: 'Interface type cannot be statically allocated.'
  This is all soooo confusing. I don't understand why all of my other text fields save into core data, but this calculated one won't.
  Any help you guys can give me would be greatly appreciated.
  :)</p>
  
  <p><strong>EDIT</strong>
  So I'm now actually getting a number from the <code>NSLog(@"%@", dataModel.finalPrice);</code> that I set up, but the value is wayyyyy off. It doesn't seem as though the math is being done correctly:</p>
  
  <pre><code>1     int dollar1 = [self.dataModel.price          intValue];
  2     int P1      = [self.dataModel.percentage     intValue];
  3     int dollar2 = [self.dataModel.price2          intValue];
  4     int P2      = [self.dataModel.percentage2    intValue];
  
  5     int totalPrice = dollar1 + dollar2;
  6     int totalPercentage = P1 + P2;
  
  7     int finalPrice = totalPrice / ((double)totalPercentage);
  8     NSDecimalNumber *finalFinalPrice = [[NSDecimalNumber alloc]initWithDouble:finalPrice];
  
  9     //Set final price to save in core data
  10     NSLog(@"setting finalPrice to core data slot");
  11    dataModel.finalPrice = finalFinalPrice;
  </code></pre>
  
  <p>I added these NSLogs to see what is being saved into core data</p>
  
  <pre><code>NSLog(@"%@", dataModel.price);
  NSLog(@"%@", dataModel.percentage);    
  NSLog(@"%@", dataModel.price2);
  NSLog(@"%@", dataModel.percentage2);
  NSLog(@"%@", dataModel.finalPrice);
  </code></pre>
  
  <p>Everything seems to be saving okay, but for example, if I have:</p>
  
  <pre><code>3.62 = dollar1;
  2.2  = P1;
  5.30 = dollar2;
  .21  = P2;
  totalPrice = dollar1 + dollar2; &lt;--this should equal 8.92
  totalPercentage = P1 + P2; &lt;--this should equal 2.41
  finalPrice = totalPrice / totalPercentage; &lt;--- this should equal 3.70
  </code></pre>
  
  <p>My output to NSLog, however, shows a value of -2147483648.
  Where in the world is that coming from?</p>
  
  <p><strong>EDIT</strong>
  I got it to save! Here's what I did:</p>
  
  <pre><code>1     NSString dollar1 = self.dataModel.price.text;
  2     NSString P1      = self.dataModel.percentage.text;
  3     NSString dollar2 = self.dataModel.price.text;
  4     NSString P2      = self.dataModel.percentage2.text;   
  
  5     double Dollar1 = [dollar1 doubleValue];
  6     double Dollar2 = [dollar2 doubleValue];
  7     double p1 = [P1 doubleValue];
  8     double p2 = [P2 doubleValue];
  
  9     double totalDollar = Dollar1 + Dollar2;
  10      double totalP = p1 + p2;
  
  11    double finalPrice = totalDollar / totalP;
        NSString *finalFinalPrice = [[NSString alloc] initWithFormat:@".2f", finalPrice];
  
  12     //Set final price to save in core data
  13     NSLog(@"setting finalPrice to core data slot");
  14    dataModel.finalPrice = finalFinalPrice;
  </code></pre>
  
  <p>Wow. This was a doozy. I can't believe the final solution was so simple!
  Thanks again for all your help!!</p>