2

NSDecimalNumber 以较少数量的零保存,例如,我在文本字段中输入 $6000,00,但在 tableview 中出现 $6,00

请帮我解决这个问题

下面是我的代码:

@property (strong, nonatomic) NSNumberFormatter *currencyFormatter;
@synthesize currencyFormatter;


- (void)viewDidLoad
{
    [super viewDidLoad];

    nameTextField.text = editGift.nameOfGift;

    currencyFormatter = [[NSNumberFormatter alloc]init];
    [currencyFormatter setNumberStyle:NSNumberFormatterCurrencyStyle];
    priceTextField.text = [currencyFormatter stringFromNumber:[editGift priceOfGift]];

}

- (IBAction)saveChanges:(id)sender {
    editGift.nameOfGift = nameTextField.text;

    [currencyFormatter setNumberStyle:NSNumberFormatterCurrencyStyle];
    NSDecimalNumber *priceAmount = [NSDecimalNumber decimalNumberWithString:priceTextField.text];
    editGift.priceOfGift = priceAmount;

    AppDelegate *myApp = (AppDelegate *) [[UIApplication sharedApplication]delegate];
    [myApp saveContext];
    [self.delegate editGiftViewControllerDidSave:self];
}

在 tableView 中显示:

NSNumberFormatter *currencyFormatter = [[NSNumberFormatter alloc]init]; 
[currencyFormatter setNumberStyle:NSNumberFormatterCurrencyStyle]; 
cell.giftCostLabel.text = [currencyFormatter stringFromNumber:[gift priceOfGift]]; 
4

1 回答 1

1

试试这个代码可能对你有帮助。

 - (IBAction)saveChanges:(id)sender {
        editGift.nameOfGift = nameTextField.text;


    NSDecimalNumber *priceAmount = [NSDecimalNumber decimalNumberWithString:priceTextField.text];

     editGift.priceOfGift =priceAmount;



        AppDelegate *myApp = (AppDelegate *) [[UIApplication sharedApplication]delegate];
        [myApp saveContext];
        [self.delegate editGiftViewControllerDidSave:self];
    }

在 Tableview 中试试这个

 NSNumberFormatter *currencyFormatter = [[NSNumberFormatter alloc] init];
    [currencyFormatter setNumberStyle: NSNumberFormatterCurrencyStyle];


cell.giftCostLabel.text = [currencyFormatter stringFromNumber:editGift.priceOfGift];
于 2012-12-19T11:10:15.363 回答