NSNumberFormatter * fmt; NSNumber * n; fmt = [ [ NSNumberFormatter alloc ] init ]; n = [ NSNumber numberWithFloat: 10 ]; [ fmt setFormatterBehavior: NSNumberFormatterBehavior10_4 ]; [ fmt setCurrencySymbol: @"$" ]; [ fmt setNumberStyle: NSNumberFormatterCurrencyStyle ]; // NSLog( @"%@",[ fmt stringFromNumber: n ]; [ fmt release ] -(IBAction)buttonPressed1:(id)sender { double currency = [Amount1.text doubleValue] + [Amount2.text doubleValue]; SumCurrency.text = [NSString stringWithFormat:@"%0.0f",currency]; }
如何将SumCurrency.text格式化为1,999而不是1999
请帮助..我是Objective-C的新手,但我的项目几乎是95%的界面构建器.
我只需要帮助上面的代码来实现..请我为此苦苦挣扎.
问候,
新手.
解决方法
我得到了答案..但是对于任何人未来的推荐
-(IBAction)buttonPressed1:(id)sender { double currency = [Amount1.text doubleValue] + [Amount2.text doubleValue]; NSNumberFormatter *numberFormatter = [[[NSNumberFormatter alloc] init] autorelease]; [numberFormatter setNumberStyle: NSNumberFormatterCurrencyStyle]; NSString *numberAsString = [numberFormatter stringFromNumber:[NSNumber numberWithInt:currency]]; SumCurrency.text = [NSString stringWithFormat:@"Converted:%@",numberAsString]; }