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