我构建了一个简单的计算器应用程序,并且正在努力添加小数点按钮,
当我按下小数点按钮时,它会添加小数,但在我输入另一个数字之后
小数点消失所以肯定是错误的。
如果有人可以专门指导我如何解决这个问题,因为我是一名初学者并且让小数点工作我会非常感激。
这是我的.h文件
#import <UIKit/UIKit.h>
int Method;
int SelectNumber;
float RunningTotal;
int currentNumber;
@interface ViewController : UIViewController
{
IBOutlet UILabel * Screen;
}
-(IBAction)Number1:(id)sender;
-(IBAction)Number2:(id)sender;
-(IBAction)Number3:(id)sender;
-(IBAction)Number4:(id)sender;
-(IBAction)Number5:(id)sender;
-(IBAction)Number6:(id)sender;
-(IBAction)Number7:(id)sender;
-(IBAction)Number8:(id)sender;
-(IBAction)Number9:(id)sender;
-(IBAction)Number0:(id)sender;
-(IBAction)Times:(id)sender;
-(IBAction)Devide:(id)sender;
-(IBAction)Subtract:(id)sender;
-(IBAction)PLus:(id)sender;
-(IBAction)Equalls:(id)sender;
-(IBAction)AllClear:(id)sender;
-(IBAction)Decimal:(id)sender;
@end
这里是.m文件中的小数代码
- (IBAction)Decimal:(UIButton *)sender
{
NSRange range = [self->Screen.text rangeOfString:@"."];
if (range.location ==NSNotFound){
self->Screen.text = [ self->Screen.text stringByAppendingString:@"."];
}
self.userIsInTheMiddleOfEnteringANumber = YES;
}
答案 0 :(得分:0)
您正在将结果打印为整数,将其更改为浮点%f,您甚至可以选择所需的小数位数:%。02f = 25.00