DaffodilGirl 2013-05-08 07:33 采纳率: 0%
浏览 1982
已采纳

无法实现计算,打印到label上

应用中点击计算按钮时的代码:

 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条回答 默认 最新

  • Chengzi_963 2013-05-08 08:04
    关注

    问题是%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];
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 matlab有关常微分方程的问题求解决
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?
  • ¥100 求三轴之间相互配合画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable