当他们点击我的应用中的计算按钮时,我正在执行以下代码:
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.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];