Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/36.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何更改小数位数和添加小数按钮iPhone计算器?_Iphone_Xcode_Calculator - Fatal编程技术网

如何更改小数位数和添加小数按钮iPhone计算器?

如何更改小数位数和添加小数按钮iPhone计算器?,iphone,xcode,calculator,Iphone,Xcode,Calculator,那么,看看下面的代码-我的第一个问题是,如何使它只有0、1或2个小数位,或者使它自动有多少个小数位?第二个问题是,如何在计算器中添加十进制按钮?它有+-/*,如何添加十进制按钮?我使用的教程在这里,这是我的代码- viewcontroller.h #import <UIKit/UIKit.h> @interface calcViewController : UIViewController { float result; IBOutlet UILabel *calculator

那么,看看下面的代码-我的第一个问题是,如何使它只有0、1或2个小数位,或者使它自动有多少个小数位?第二个问题是,如何在计算器中添加十进制按钮?它有+-/*,如何添加十进制按钮?我使用的教程在这里,这是我的代码-

viewcontroller.h

 #import <UIKit/UIKit.h>

@interface calcViewController : UIViewController {

float result;
IBOutlet UILabel *calculatorScreen;
int currentOperation;
float currentNumber;

}


-(IBAction)buttonDigitPressed:(id)sender;
-(IBAction)buttonOperationPressed:(id)sender;
-(IBAction)cancelInput;
-(IBAction)cancelOperation;



@end

您可以在输出上使用此选项

Float *number = 2.2f;
NSLog(@"%.2f",number);

关于该点,您需要注意的另一点是,您不希望在计算中有多个点,例如10.345.1123.5。简单地说,您希望它也是一个合法的浮点数

话虽如此,您可以使用iAction(记住将其链接到您的故事板或xib文件)

虽然我们可能在同一个项目上进行,但也可能完全不同(您自己从头开始),因此我将介绍一些代码

您可以用默认id替换UIButton,但最好是静态替换它,以便为您自己或查看您的代码的任何其他人澄清

NSRange顾名思义,标记该范围,该范围将是您的计算显示文本(例如1235.3568),在本例中它所针对的字符串范围是“.”。因此,如果在文本范围中找不到NSNotfound(rangeOfString“.”),您将使用函数stringByAppendingString将当前显示文本附加“.”:@“”,没有其他功能,因此如果已找到“”,则不会发生任何功能,从而解决了显示器上多点的问题

userIsInTheMiddleOfEnteringANumber是一个用于解决在您的显示器中显示0(例如06357)问题的BOOL,如果您有方法更改它,请用您自己的方法名称替换我的方法名称

关于显示器,由于我使用的方法与您的不同,我无法在这方面提供任何帮助或指导。

这是我的解决方案:

ViewController.h

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController {
    float result;
    IBOutlet UILabel *TextInput;
    int currentOperation;
    float currentNumber;
    BOOL userInTheMiddleOfEnteringDecimal;
}

- (IBAction)buttonDigitPressed:(id)sender;
- (IBAction)buttonOperationPressed:(id)sender;
- (IBAction)cancelInput;
- (IBAction)cancelOperation;
- (IBAction)dotPressed;
@end

希望这有帮助。这包括按钮中的小数点…

如果有人能提供帮助,我将不胜感激。无论您想在哪里显示输出,都需要使用%.2f
-(IBAction)decimalPressed:(UIButton *)sender
{
 NSRange range = [self.display.text rangeOfString:@"."];
if (range.location ==NSNotFound){
self.display.text = [ self.display.text stringByAppendingString:@"."];
}
self.userIsInTheMiddleOfEnteringANumber = YES;
}
#import <UIKit/UIKit.h>

@interface ViewController : UIViewController {
    float result;
    IBOutlet UILabel *TextInput;
    int currentOperation;
    float currentNumber;
    BOOL userInTheMiddleOfEnteringDecimal;
}

- (IBAction)buttonDigitPressed:(id)sender;
- (IBAction)buttonOperationPressed:(id)sender;
- (IBAction)cancelInput;
- (IBAction)cancelOperation;
- (IBAction)dotPressed;
@end
#import "ViewController.h"

@interface ViewController ()


@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

- (IBAction)buttonDigitPressed:(id)sender {
    if(!userInTheMiddleOfEnteringDecimal)
    {
        currentNumber = currentNumber*10 + (float)[sender tag];
        TextInput.text = [NSString stringWithFormat:@"%d",(int)currentNumber];
    }
    else{
        TextInput.text= [TextInput.text stringByAppendingString:[NSString stringWithFormat:@"%d",[sender tag]]];
        currentNumber = [TextInput.text floatValue];
    }
}

- (IBAction)buttonOperationPressed:(id)sender {
    if (currentOperation == 0) result = currentNumber;
    else {
        switch (currentOperation) {
            case 1:
                result = result + currentNumber;
                break;
            case 2:
                result = result - currentNumber;
                break;
            case 3:
                result = result * currentNumber;
                break;
            case 4:
                result = result / currentNumber;
                break;
            case 5:
                currentOperation = 0;
                break;
            default:
                break;
        }
    }
    currentNumber = 0;
    TextInput.text = [NSString stringWithFormat:@"%.2f",result];
    if ([sender tag] == 0) result = 0;
    currentOperation = [sender  tag];
    userInTheMiddleOfEnteringDecimal = NO;
}

-(IBAction)cancelInput{
    currentNumber = (int)(currentNumber/10);
    TextInput.text = [NSString stringWithFormat:@"%.2f",currentNumber];;
}

-(IBAction)cancelOperation{
    currentNumber = 0;
    TextInput.text = @"0";
    userInTheMiddleOfEnteringDecimal = NO;
}
- (IBAction)dotPressed{
    if(!userInTheMiddleOfEnteringDecimal){
        userInTheMiddleOfEnteringDecimal = YES;
        TextInput.text= [TextInput.text stringByAppendingString:@"."];
    }
}

@end