Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 将数字格式化为百分比_Objective C_Cocoa Touch - Fatal编程技术网

Objective c 将数字格式化为百分比

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]; 您需要使用%%以打印格式字符串中的百分号。按如下方式生

我已经编写了这段代码,我想将数字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] );