Friday, 9 August 2013

iPhone Calculator App Unwanted Decimal Places

iPhone Calculator App Unwanted Decimal Places

I recently made watched a tutorial for an calculator iPhone app. It works
however I am having an issue with unwanted decimal places, Meaning my app
will put a dot after my number and have like 6 0s after it. I looked up
how to fix this and tried what I saw, however it only works when I type in
the first number in. When I click the Multiplication, Division,
Subtraction, or Addition button they reappear and when it shows the answer
it includes the unwanted decimals too. My code is below, Please Help me
Thanks!
ViewController.h
//
// ViewController.h
// Simple Calculator
//
// Created by Michael on 2013-08-08.
// Copyright (c) 2013 com.company. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController {
float result;
IBOutlet UILabel *calculatorScreen;
int currentOperation;
float currentNumber;
BOOL userInTheMiddleOfEnteringDecimal;
}
-(IBAction)buttonDigitPressed:(id)sender;
-(IBAction)buttonOperationPressed:(id)sender;
-(IBAction)cancelInput;
-(IBAction)cancelOperation;
@end
ViewController.m
//
// ViewController.m
// Simple Calculator
//
// Created by Michael on 2013-08-08.
// Copyright (c) 2013 com.company. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
-(IBAction)buttonDigitPressed:(id)sender {
if (!userInTheMiddleOfEnteringDecimal)
{
currentNumber = currentNumber *10 + (float ) [sender tag];
calculatorScreen.text = [NSString
stringWithFormat:@"%2d",(int)currentNumber];
}
else{
calculatorScreen.text= [calculatorScreen.text
stringByAppendingString:[NSString stringWithFormat:@"%d",[sender
tag]]];
currentNumber = [calculatorScreen.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;
}
}
currentNumber = 0;
calculatorScreen.text = [NSString stringWithFormat:@"%2f",result];
if ([sender tag] == 0) result=0;
currentOperation = [sender tag];
userInTheMiddleOfEnteringDecimal = NO;
}
-(IBAction)cancelInput {
currentNumber = (int)(currentNumber/10);
calculatorScreen.text = [NSString
stringWithFormat:@"%.2f",currentNumber];;
}
-(IBAction) cancelOperation {
currentNumber = 0;
calculatorScreen.text = @"0";
userInTheMiddleOfEnteringDecimal = NO;
}
- (IBAction)dotPressed{
if(!userInTheMiddleOfEnteringDecimal){
userInTheMiddleOfEnteringDecimal = YES;
calculatorScreen.text= [calculatorScreen.text
stringByAppendingString:@"."];
}
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
Thanks :)

No comments:

Post a Comment