iphone – 无法进行计算并打印出标签
发布时间:2020-12-14 17:12:19 所属栏目:百科 来源:网络整理
导读:当他们点击我的应用中的计算按钮时,我正在执行以下代码: float sqft = ([textfield1.text floatValue]); float thick= ([textfield2.text floatValue]); float cos = ([textfield3.text floatValue]); float eff = ([textfield4.text floatValue]); float n
当他们点击我的应用中的计算按钮时,我正在执行以下代码:
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]; (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |