self.numberFormatter = [[NSNumberFormatter alloc] init]; [self.numberFormatter setNumberStyle:NSNumberFormatterCurrencyStyle]; [self.numberFormatter setCurrencySymbol:@""]; [self.numberFormatter setLocale:[NSLocale currentLocale]];
//In DataManager sharedClass .h
- (void)setupCapital:(float)Capital;
- (float)readCapital;
(NSString*)formatCapital:(NSNumber *)Capital;
// In DataManager sharedClass .m
@property (assign, nonatomic) float theCapital;
- (NSString *) formatCapital:(NSNumber *)Capital {
return [self.numberFormatter stringFromNumber:Capital];
}
- (void)formatCapital:(float)Capital {
self.theCapital = Capital;
}
- (float) readCapital {
return self.theCapital;
}
// write the number
float numberDecimal = [[DataManager sharedClass] readCapital];
self.labelCapital.text = [[DataManager sharedClass] formatCapital:[NSNumber numberWithFloat:numberDecimal]];
为什么如果我插入 123456,89 我在 labelCapital 中正确地有值 123.456,89
如果我插入 1234567,89 我有 labelCapital 1.234.567,88
如果我插入 123456789,89 我在 labelCapital 123.456.792,00
有什么提示吗?