Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

How can I add decimals to my calculator? Percentages as well?

I am building a simple calculator app. I have implemented my calculator this way: I have the viewcontroller.h and .m, I also have my "CalculatorBrain.h and .m" That is where I implement the addition and substraction, multiplication. I use the symbol or the text of the button to read it and calculate the value. The question is, if I have 1.5 plus 1.5, it says the answer is 2. It only takes the 1 and the 1. How do I add decimals? Also, if possible, how would I do percentages?

This is my Calculator Brain.m:

#import "CalculatorBrain.h"

const NSString *Delete = @"D";


@implementation CalculatorBrain

- (void)setOperand:(double)anDouble

{

operand = anDouble;

}

- (void)performWaitingOperation

{

if ([@"+"isEqual:waitingOperation]) {

operand = waitingOperand + operand;

} else if ([@"-" isEqual:waitingOperation]) {

operand = waitingOperand - operand;

} else if ([@"×" isEqual:waitingOperation]) {

operand = waitingOperand * operand;

} else if ([@"÷" isEqual:waitingOperation]) {

if (operand) {

operand = waitingOperand / operand;

}

}





}

- (double)performOperation:(NSString *)operation;

{

if ([operation isEqual:@"√"]) {

operand = sqrt(operand);

} else {

[selfperformWaitingOperation];

waitingOperation = operation;

waitingOperand = operand;


}

returnoperand;

}




@end



And this is my View Controller.m. Any suggestions?:

#import "CalculatorViewController.h"


@implementation CalculatorViewController

- (CalculatorBrain *)brain

{

if (!brain) {

brain= [[CalculatorBrainalloc] init];

}

return brain;

}


- (IBAction)digitPressed:(UIButton *)sender;

{

NSString *digit = [[sender titleLabel] text];

if (userIsInTheMiddleOfTypingANumber) {

[displaysetText:[[displaytext] stringByAppendingString:digit]];


} else {

[display setText:digit];

userIsInTheMiddleOfTypingANumber = YES;

}


}

- (IBAction)operationPressed:(UIButton *)sender;

{

if (userIsInTheMiddleOfTypingANumber) {

[[self brain] setOperand:[[display text] doubleValue]];

userIsInTheMiddleOfTypingANumber = NO;

}

NSString *operation = [[sender titleLabel] text];

double result = [[self brain] performOperation:operation];

[displaysetText:[NSStringstringWithFormat:@"%g", result]];

}

- (void) runTimer;

{

// This starts the timer which fires the showActivity

// method every 0.5 seconds

myTicker = [NSTimerscheduledTimerWithTimeInterval: 0.5

target: self

selector: @selector(showActivity)

userInfo: nil

repeats: YES];


}

- (void)showActivity;

{

NSDateFormatter *formatter =

[[NSDateFormatteralloc] init];

NSDate *date = [NSDate date];


// This will produce a time that looks like "12:15:00 PM".

[formatter setTimeStyle:NSDateFormatterMediumStyle];


// This sets the label with the updated time.

[clockLabel setText:[formatter stringFromDate:date]];

}

// Implement viewDidLoad to do additional

// setup after loading the view, typically from a nib.

- (void)viewDidLoad {

[superviewDidLoad];


// This calls the runTimer method after loading

// SimpleClockViewController.xib

[self runTimer];

}


// CalcController.m

// SimpleCalc

//

// Created by Chris Ball (chris.m.ball@gmail.com) on 1/17/08.

// Copyright 2008, Chris Ball, Strainthebrain.Blogspot.Com.

// All rights reserved.

//

- (IBAction)resetEverythingPressed:(UIButton *)sender;

{

if (userIsInTheMiddleOfTypingANumber) {

[[self brain] setOperand:[[display text] doubleValue]];

userIsInTheMiddleOfTypingANumber = NO;

}

NSString *operation = [[sender titleLabel] text];

double result = [[self brain] performOperation:operation];

[displaysetText:[NSStringstringWithFormat:@"0", result]];

}



@end

Oh, by the way, it tells the time. And if you want a screenshot, so you can see how it works out...

User uploaded file

Xcode 4.2 Beta-OTHER, Other OS, I am running Mac OSX Lion

Posted on Jun 12, 2011 10:52 AM

Reply
4 replies

How can I add decimals to my calculator? Percentages as well?

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.