NSDecimalNumber

时间:2012-12-19 10:45:01

标签: ios core-data nsdecimalnumber

NSDecimalNumber以较少数量的零保存,例如,我在textfield $ 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]]; 

1 个答案:

答案 0 :(得分: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];