DaffodilGirl 于 2013.05.08 15:33 提问

`````` float sqft = ([textfield1.text floatValue]);
float thick= ([textfield2.text floatValue]);
float cos = ([textfield3.text floatValue]);
float eff = ([textfield4.text floatValue]);

float num = ((thick*.5)*sqft)/eff;
float cost = (num*cos);
float costft = (cost/sqft);

label1.text = [NSString stringWithFormat:@"%2.f",num];
label2.text = [NSString stringWithFormat:@"%2.f",cost];
label3.text = [NSString stringWithFormat:@"%2.f",costft];
``````

2个回答

Chengzi_963   2013.05.08 16:04

%2.f返回的答案是2 数字的格式，如果结果小于或等于0.5。那么答案就会得0

``````float sqft = ([textfield1.text floatValue]);
float thick= ([textfield2.text floatValue]);
float cos = ([textfield3.text floatValue]);
float eff = ([textfield4.text floatValue]);

float num = ((thick*.5)*sqft)/eff;
float cost = (num*cos);
float costft = (cost/sqft);

label1.text = [NSString stringWithFormat:@"%2f",num];
label2.text = [NSString stringWithFormat:@"%2f",cost];
label3.text = [NSString stringWithFormat:@"%2f",costft];
``````
KindAzrael   2013.05.08 22:24

%2.f 格式表示 小数位为0位（<=0.5舍掉反之入1） 整数位至少2位 不够的用前导空白填充