我为 iPad 的计算器应用程序编写了这段代码,但我找不到解决十进制数字的方法。例如,当我尝试解决:4.5 + 0.5 时,它只会给我 4 个答案。我知道这其中缺少一些东西。
感谢那些传入的回复。
提前干杯!
- (IBAction)equalsPressed {
self.typingNumber = NO;
self.secondNumber = [self.calculatorDisplay.text intValue];
int result = 0;
if ([self.operation isEqualToString:@"+"]) {
result = self.firstNumber + self.secondNumber;
}
else if ([self.operation isEqualToString:@"-"]) {
result = self.firstNumber - self.secondNumber;
}
else if ([self.operation isEqualToString:@"*"]) {
result = self.firstNumber * self.secondNumber;
}
else if ([self.operation isEqualToString:@"/"]) {
result = self.firstNumber / self.secondNumber;
}
self.calculatorDisplay.text = [NSString stringWithFormat:@"%2.d", result];
self.displayLabel.text = self.calculatorDisplay.text;
}
- (IBAction) clearPressed: (id)sender {
self.calculatorDisplay.text = @"";
self.firstNumber = [self.calculatorDisplay.text intValue];
self.operation = [sender currentTitle];
}
- (IBAction)backspaceButton: (id)sender {
self.displayLabel.text = [self.displayLabel.text substringToIndex:self.displayLabel.text.length - 1];
}
- (IBAction)decimalPressed:(id)sender {
NSString *currentText = self.displayLabel.text;
if ([currentText rangeOfString:@"." options:NSBackwardsSearch].length == 0) {
self.displayLabel.text = [self.displayLabel.text stringByAppendingString:@"."];
}
}