我已编写此代码,我想将数字z格式化为百分比。
float l = ([textField2.text floatValue]);
float g = ([textField1.text floatValue]);
float x = l/1.23;
float y = x-g;
float z = y/l;
label.text = [[NSString alloc] initWithFormat:@"%2.2f \%",z];
答案 0 :(得分:6)
使您的代码如下。
label.text = [[NSString alloc] initWithFormat:@"%2.2f %%",(z*100)];
答案 1 :(得分:4)
NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
[numberFormatter setNumberStyle:NSNumberFormatterPercentStyle];
[numberFormatter setMinimumFractionDigits:2]; //optional
....
NSNumber *number = [NSNumber numberWithFloat:0.435];
NSLog(@"%@", [numberFormatter stringFromNumber:number] );
43,50%
答案 2 :(得分:3)
您需要使用%%
才能在格式字符串中打印百分号。