應用中點擊計算按鈕時的代碼:
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];
但是label只返回0?我設置了標簽為字符串。但是結果是一樣的。
問題是%2.f。
%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];