我有 UILabel 我在其中显示浮点值我希望如果值以千为单位如 $1000.00 那么标签应该显示 $1,000.00
使用以下方式我的代码显示
$1000.00
labelOne.text=[NSString stringWithFormat:@"$ %.2f",f_Cost_of_Vacination_NV];
您需要使用NSNumberFormatter
NSString *formatted = [NSNumberFormatter localizedStringFromNumber:@(f_Cost_of_Vacination_NV) numberStyle:NSNumberFormatterCurrencyStyle];
NSLog(@"%@", formatted); // will log it as 1,000.00
您需要使用数字样式 Currency.this 可能会解决您的问题,使用 NSNumberFormatter... 并使用此行设置货币样式
NSNumberFormatterCurrencyStyle.
看到这个
NSNumber *number=[[NSNumber alloc]initWithInt:1000];
NSString *No = [NSNumberFormatter localizedStringFromNumber:number numberStyle:NSNumberFormatterCurrencyStyle];
NSLog(@"%@", No);
UILabel * text=[NSString stringWithFormat:@"%@",No];
NSLog(@"%@",text);
你可以这样做:
double currency=1000;
NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init] ;
[numberFormatter setNumberStyle: NSNumberFormatterCurrencyStyle];
NSString *numberAsString = [numberFormatter stringFromNumber:[NSNumber numberWithInt:currency]];
NSLog(@" print Converted:%@",numberAsString]);
快乐编码!
试试这个
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];