xcode 将 NSString 转换为 NSDecimalNumber

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14371907/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 02:32:19  来源:igfitidea点击:

Convert NSString to NSDecimalNumber

xcodecore-datansstringnsdecimalnumber

提问by StreaminJA

In my Core Data model, "amount" is an attribute in entity "Tracking". How do I convert amount to NSDecimalNumber throughout the code? I'm sure someone with more experience can do this in 3 minutes where I have been looking at it for days.

在我的 Core Data 模型中,“amount”是实体“Tracking”中的一个属性。如何在整个代码中将数量转换为 NSDecimalNumber?我相信有更多经验的人可以在 3 分钟内完成这件事,而我已经看了好几天了。

- (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];
}

回答by gerrytan

NSDecimalNumber has following method which you can use to convert your NSString into it:

NSDecimalNumber 具有以下方法,您可以使用它来将您的 NSString 转换为它:

+ (NSDecimalNumber *)decimalNumberWithString:(NSString *)numericString

For example:

例如:

NSString *foo = @"1.0";
NSDecimalNumber *cow = [NSDecimalNumber decimalNumberWithString:foo];

Full reference of the class is available here: http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSDecimalNumber_Class/Reference/Reference.html#//apple_ref/occ/cl/NSDecimalNumber

该类的完整参考可在此处获得:http: //developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSDecimalNumber_Class/Reference/Reference.html#//apple_ref/occ/cl/ NSDecimalNumber

回答by hd1

Here you go:

干得好:

NSString *order; 
NSDecimalNumber *decimal = [NSDecimalNumber decimalNumberWithString:order];