Objective c 将数字格式化为百分比
我已经编写了这段代码,我想将数字z格式化为百分比Objective c 将数字格式化为百分比,objective-c,cocoa-touch,Objective C,Cocoa Touch,我已经编写了这段代码,我想将数字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]; 您需要使用%%以打印格式字符串中的百分号。按如下方式生
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];
您需要使用
%%
以打印格式字符串中的百分号。按如下方式生成代码
label.text = [[NSString alloc] initWithFormat:@"%2.2f %%",(z*100)];
43,50%谢谢您的回答,但我想将数字的格式从浮点设置为百分比。
NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
[numberFormatter setNumberStyle:NSNumberFormatterPercentStyle];
[numberFormatter setMinimumFractionDigits:2]; //optional
....
NSNumber *number = [NSNumber numberWithFloat:0.435];
NSLog(@"%@", [numberFormatter stringFromNumber:number] );