iphone – 无法进行计算并打印出标签

前端之家收集整理的这篇文章主要介绍了iphone – 无法进行计算并打印出标签前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
当他们点击我的应用中的计算按钮时,我正在执行以下代码

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];

猜你在找的Xcode相关文章