我有UILabel,其中我显示浮动值我希望如果价值是千元,如$ 1000.00 然后标签应显示$ 1,000.00
使用以下方式我的代码显示
$ 1000.00包装
labelOne.text=[NSString stringWithFormat:@"$ %.2f",f_Cost_of_Vacination_NV];
答案 0 :(得分:0)
您需要使用NSNumberFormatter
NSString *formatted = [NSNumberFormatter localizedStringFromNumber:@(f_Cost_of_Vacination_NV) numberStyle:NSNumberFormatterCurrencyStyle];
NSLog(@"%@", formatted); // will log it as 1,000.00
答案 1 :(得分:0)
您需要使用数字样式货币。这可能会解决您的问题使用NSNumberFormatter ...并使用此行来设置货币风格
NSNumberFormatterCurrencyStyle.
见这个
NSNumber *number=[[NSNumber alloc]initWithInt:1000];
NSString *No = [NSNumberFormatter localizedStringFromNumber:number numberStyle:NSNumberFormatterCurrencyStyle];
NSLog(@"%@", No);
UILabel * text=[NSString stringWithFormat:@"%@",No];
NSLog(@"%@",text);
答案 2 :(得分:0)
你可以这样做:
double currency=1000;
NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init] ;
[numberFormatter setNumberStyle: NSNumberFormatterCurrencyStyle];
NSString *numberAsString = [numberFormatter stringFromNumber:[NSNumber numberWithInt:currency]];
NSLog(@" print Converted:%@",numberAsString]);
快乐的编码!
答案 3 :(得分:-1)
尝试这个
float value = 200000;
NSNumberFormatter * formatter = [NSNumberFormatter new];
[formatter setNumberStyle:NSNumberFormatterDecimalStyle];
[formatter setMaximumFractionDigits:2]; // Set this if you need 2 digits
NSString * newString = [formatter stringFromNumber:[NSNumber numberWithFloat:value]];
labelOne.text=[NSString stringWithFormat:@"$ %@",newString];