在我的核心數據模型中,「amount」是實體「Tracking」中的一個屬性。如何在整個代碼中將金額轉換爲NSDecimalNumber?我相信有更多經驗的人可以在3分鐘內做到這一點,我已經看了好幾天。將NSString轉換爲NSDecimalNumber
- (IBAction)save {
if (![amount.text isEqualToString:@""]) {
Tracking *tracking = [NSEntityDescription insertNewObjectForEntityForName:@"Tracking"
inManagedObjectContext:self.managedObjectContext];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *weightEntity = [NSEntityDescription entityForName:@"Tracking" inManagedObjectContext:self.managedObjectContext];
[fetchRequest setEntity:weightEntity];
NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"timestamp" ascending:NO];
[fetchRequest setSortDescriptors:[NSArray arrayWithObject:sortDescriptor]];
NSError *error = nil;
NSArray *result = [self.managedObjectContext executeFetchRequest:fetchRequest error:&error];
Tracking *person = [result objectAtIndex:0];
NSLog(@"%@",person.date);
NSDate *date = [NSDate date];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc]init];
[dateFormat setDateFormat:@"MMM dd, yyyy"];
NSString *dateString = [dateFormat stringFromDate:date];
//NSString *dateString = @"Jan 13 2013";
//Tracking *track = [self.fetchedResultsController objectAtIndexPath:nil];
tracking.note = note.text;
tracking.amount = amount.text;
tracking.title = [segmentedChoice titleForSegmentAtIndex:segmentedChoice.selectedSegmentIndex];
tracking.date = dateString;
tracking.timestamp = date;
if (![person.date isEqualToString:dateString]) {
if ([[segmentedChoice titleForSegmentAtIndex:segmentedChoice.selectedSegmentIndex] isEqualToString:@"Food/Drink"]) {
tracking.pic = @"Drink";
tracking.total = [NSString stringWithFormat:@"%.2f", 0.00 - [amount.text floatValue]];
NSLog(@"%f",[person.total floatValue]);
NSLog(@"%f", [amount.text floatValue]);
//tracking.total = @"1000.00";
}
else if ([[segmentedChoice titleForSegmentAtIndex:segmentedChoice.selectedSegmentIndex] isEqualToString:@"Other"]) {
tracking.total = [NSString stringWithFormat:@"%.2f",0.00 - [amount.text floatValue]];
tracking.pic = [segmentedChoice titleForSegmentAtIndex:segmentedChoice.selectedSegmentIndex];
}
else {
tracking.pic = [segmentedChoice titleForSegmentAtIndex:segmentedChoice.selectedSegmentIndex];
tracking.total = [NSString stringWithFormat:@"%.2f",0.00 + [amount.text floatValue]];
}
}
else {
if ([[segmentedChoice titleForSegmentAtIndex:segmentedChoice.selectedSegmentIndex] isEqualToString:@"Food/Drink"]) {
tracking.pic = @"Drink";
tracking.total = [NSString stringWithFormat:@"%.2f",[person.total floatValue] - [amount.text floatValue]];
NSLog(@"%f",[person.total floatValue]);
NSLog(@"%f", [amount.text floatValue]);
//tracking.total = @"1000.00";
}
else if ([[segmentedChoice titleForSegmentAtIndex:segmentedChoice.selectedSegmentIndex] isEqualToString:@"Other"]) {
tracking.total = [NSString stringWithFormat:@"%.2f",[person.total floatValue] - [amount.text floatValue]];
tracking.pic = [segmentedChoice titleForSegmentAtIndex:segmentedChoice.selectedSegmentIndex];
}
else {
tracking.pic = [segmentedChoice titleForSegmentAtIndex:segmentedChoice.selectedSegmentIndex];
tracking.total = [NSString stringWithFormat:@"%.2f",[person.total floatValue] + [amount.text floatValue]];
}
}
NSLog(@"\n Title: %@ \n Amount: %@ \n Note: %@ \n Date: %@ \n Picture: %@.png \n Total: %@ \n TimeStamp: %@", tracking.title, tracking.amount, tracking.note,tracking.date,tracking.pic, tracking.total, tracking.timestamp);
[self.managedObjectContext save:nil];
/* UIAlertView *alert = [[UIAlertView alloc] initWithTitle:[NSString stringWithFormat:@"%@ Saved",tracking.title] message:[NSString stringWithFormat:@"Amount: %@ \n Note: %@",tracking.amount,tracking.note] delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[alert show];*/
[amount resignFirstResponder];
[note resignFirstResponder];
[self dismissViewControllerAnimated:YES completion:nil];
}
它仍然具有進一步向下代碼的浮點值嗎? – StreaminJA
使用[cow doubleValue]獲取原始值。 – hd1