我已經寫了這段代碼,我想將數字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)];
你是個小神!謝謝!! :d –
NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
[numberFormatter setNumberStyle:NSNumberFormatterPercentStyle];
[numberFormatter setMinimumFractionDigits:2]; //optional
....
NSNumber *number = [NSNumber numberWithFloat:0.435];
NSLog(@"%@", [numberFormatter stringFromNumber:number]);
43,50%
謝謝你的答案,但我想數字格式從浮子個百分點。 –