【发布时间】:2011-06-19 14:33:48
【问题描述】:
对整个 iPhone 开发场景来说都是全新的。我只是在练习,尝试创建一个基本的计算器,我可以添加简单的数字,但我想支持小数位。
到目前为止,这是我的代码:
- (IBAction) calculate
{
double number1 = ([textField.text doubleValue]);
double answer = number1+([textField2.text doubleValue]);
label.text = [[NSString alloc] initWithFormat:@"%2.f", answer];
}
- (IBAction) clear
{
textField.text = @"";
textField2.text = @"";
label.text = @"";
}
非常感谢任何帮助。
【问题讨论】:
-
@Seva 如何支持小数位。
-
您需要执行以下操作以避免内存泄漏:
label.text = [NSString stringWithFormat:@"%2.f", answer];。只是想我会指出这一点。 :) -
那么,这是一个 C 问题,还是与 iOS 相关的问题?你不是真的在这里问问题。你被什么困住了?
-
现在没有?好像数学已经是浮点数了。如果您在输出时没有得到小数点后的数字,请使用
%2.f格式说明符。以普通的 %f 开头。 -
顺便说一句,
double answer = number1+([textField2.text doubleValue]);中不需要括号。可以写成double answer = number1+[textField2.text doubleValue];。
标签: iphone objective-c calculator