Xcode 将NSString转换为NSDecimalNumber
在我的核心数据模型中,数量是实体跟踪中的一个属性。如何在整个代码中将金额转换为NSDecimalNumber?我相信有经验的人可以在3分钟内完成这项工作,而我已经看了几天了Xcode 将NSString转换为NSDecimalNumber,xcode,core-data,nsstring,nsdecimalnumber,Xcode,Core Data,Nsstring,Nsdecimalnumber,在我的核心数据模型中,数量是实体跟踪中的一个属性。如何在整个代码中将金额转换为NSDecimalNumber?我相信有经验的人可以在3分钟内完成这项工作,而我已经看了几天了 - (IBAction)save { if (![amount.text isEqualToString:@""]) { Tracking *tracking = [NSEntityDescription insertNewObjectForEntityForName:@"Tracking"
- (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];
}
NSDecimalNumber具有以下方法,您可以使用这些方法将NSString转换为它:
+ (NSDecimalNumber *)decimalNumberWithString:(NSString *)numericString
例如:
NSString *foo = @"1.0";
NSDecimalNumber *cow = [NSDecimalNumber decimalNumberWithString:foo];
该类的完整参考可在此处找到:NSDecimalNumber具有以下方法,您可以使用这些方法将NSString转换为它:
+ (NSDecimalNumber *)decimalNumberWithString:(NSString *)numericString
例如:
NSString *foo = @"1.0";
NSDecimalNumber *cow = [NSDecimalNumber decimalNumberWithString:foo];
课程的完整参考资料可在此处找到:给你:
NSString *order;
NSDecimalNumber *decimal = [NSDecimalNumber decimalNumberWithString:order];
给你:
NSString *order;
NSDecimalNumber *decimal = [NSDecimalNumber decimalNumberWithString:order];
使用[cow doubleValue]获取原始值。使用[cow doubleValue]获取原始值。您好,我的字符串是@12.30,然后它返回12.3。它删除零我不想删除零。我该怎么做请帮助我。谢谢你,我尝试过这种方法,但它与修复分词一起工作,我想要动态的,所以我如何才能做到这一点。?还请检查此Hi我的字符串是@12.30,然后返回12.3它删除零我不想删除零我如何可以这样做,请帮助我。谢谢你,我尝试过这种方法,但它与修复分词一起工作,我想要动态的,所以我如何才能做到这一点。?也检查一下这个