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 {
        [self performWaitingOperation];
        waitingOperation = operation;
        waitingOperand = operand;
    return operand;
@end
And this is my View Controller.m. Any suggestions?:
#import "CalculatorViewController.h"
@implementation CalculatorViewController
- (CalculatorBrain *)brain
    if (!brain) {
        brain= [[CalculatorBrain alloc] init];
    return brain;
- (IBAction)digitPressed:(UIButton *)sender;
    NSString *digit = [[sender titleLabel] text];
    if (userIsInTheMiddleOfTypingANumber) {
        [display setText:[[display text] 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];
    [display setText:[NSString stringWithFormat:@"%g", result]];
- (void) runTimer;
    // This starts the timer which fires the showActivity
    // method every 0.5 seconds
    myTicker = [NSTimer scheduledTimerWithTimeInterval: 0.5
                                                target: self
                                              selector: @selector(showActivity)
                                              userInfo: nil
                                               repeats: YES];
- (void)showActivity;
    NSDateFormatter *formatter =
    [[NSDateFormatter alloc] 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 {
    [super viewDidLoad];
    // This calls the runTimer method after loading
    // SimpleClockViewController.xib
    [self runTimer];
//  CalcController.m
//  SimpleCalc
//  Created by Chris Ball ([email protected]) 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];
    [display setText:[NSString stringWithFormat:@"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...

For percentages, first make sure the number is < 0, then multiply it by 100, add a % sign and you're done
        double displayValue = [display.text doubleValue];
        if(displayValue < 0)
            displayValue *= 100;
            [display setText:[@"%" stringByAppendingFormat:@"%g", displayValue]];

Similar Messages

  • How can I add sales TAX at the end of my calculations?

    How can I add sales TAX at the end of my calculations?

    We currently do not support adding sales tax.
    Randy

  • How can I add more than one same spry menu (eg. collapsible menu)  with in different styles (font size, color, background, etc) on current page?

    How can I add more than one same spry menu (eg. collapsible menu)  with in different styles (font size, color, background, etc) on current page?

    Hi Nancy,
    This screenshot was only for imagination. A part of the code (not all) is below.  In the code there are some background images but they are not seem in live mode.
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title></title>
    <link href="css/my_site.css" rel="stylesheet" type="text/css" />
    <link href="SpryAssets/SpryTabbedPanels.css" rel="stylesheet" type="text/css"/>
    <link href="SpryAssets/SpryCollapsiblePanel.css" rel="stylesheet" type="text/css" />
    <script src="SpryAssets/SpryTabbedPanels.js" type="text/javascript"></script>
    <script src="SpryAssets/SpryCollapsiblePanel.js" type="text/javascript"></script>
    <style>
    #CollapsiblePanel1 .CollapsiblePanelOpen .CollapsiblePanelTab {
        background-color: #003366;
        font-size: 18px;
        line-height: 52px;
        color: #FFF;
    #CollapsiblePanel1 .CollapsiblePanelTabHover .CollapsiblePanelTab {
        background-color: #003366;
        color: #FFF;
        text-shadow: 1px 1px #000;
        font-weight: bold;
        line-height: 52px;
    #CollapsiblePanel1 .CollapsiblePanelClosed .CollapsiblePanelTab  {
        background-color: #C3CFDF;
        border-radius: 5px 5px 0px 0px;
        color: #999
        text-shadow: 1px 1px #000;
        font-family: "Trebuchet MS", Arial, Helvetica, sans-serif;
        font-size: 18px;
        font-weight: bold;
        line-height: 52px;
    #CollapsiblePanel2 .CollapsiblePanelOpen .CollapsiblePanelTab {
        background-image: url(images/international.jpg);
        background-repeat: no-repeat;
        font-size: 18px;
        line-height: 52px;
        color: #FFF;
    #CollapsiblePanel2 .CollapsiblePanelTabHover .CollapsiblePanelTab {
        background-color: #003366;
        color: #FFF;
        text-shadow: 1px 1px #000;
        font-weight: bold;
        background-image: url(images/TR_Col-WEB.png);
        background-repeat: no-repeat;
        line-height: 52px;
    #CollapsiblePanel2 .CollapsiblePanelClosed .CollapsiblePanelTab  {
        background-color: #C3CFDF;
        border-radius: 5px 5px 0px 0px;
        color: #999
        text-shadow: 1px 1px #000;
        font-family: "Trebuchet MS", Arial, Helvetica, sans-serif;
        font-size: 18px;
        font-weight: bold;
        background-image: url(images/TR_Gray2-WEB.png);
        background-repeat: no-repeat;
        line-height: 52px;
    #CollapsiblePanel2 .CollapsiblePanelContent {
        background-color: blue;
    #CollapsiblePanel3 .CollapsiblePanelOpen .CollapsiblePanelTab {
        background-image: url(images/TR_Col-WEB.png);
        background-repeat: no-repeat;
        font-size: 18px;
        line-height: 52px;
        color: #FFF;
    #CollapsiblePanel3 .CollapsiblePanelTabHover .CollapsiblePanelTab {
        background-color: #003366;
        color: #FFF;
        text-shadow: 1px 1px #000;
        font-weight: bold;
        background-image: url(images/TR_Col-WEB.png);
        background-repeat: no-repeat;
        line-height: 52px;
    #CollapsiblePanel3 .CollapsiblePanelClosed .CollapsiblePanelTab  {
        background-color: #C3CFDF;
        border-radius: 5px 5px 0px 0px;
        color: #999
        text-shadow: 1px 1px #000;
        font-family: "Trebuchet MS", Arial, Helvetica, sans-serif;
        font-size: 18px;
        font-weight: bold;
        background-image: url(images/TR_Gray2-WEB.png);
        background-repeat: no-repeat;
        line-height: 52px;
    #CollapsiblePanel4 .CollapsiblePanelOpen .CollapsiblePanelTab {
        background-image: url(images/TR_Col-WEB.png);
        background-repeat: no-repeat;
        font-size: 18px;
        line-height: 52px;
        color: #FFF;
    #CollapsiblePanel4 .CollapsiblePanelTabHover .CollapsiblePanelTab {
        background-color: #003366;
        color: #FFF;
        text-shadow: 1px 1px #000;
        font-weight: bold;
        background-image: url(images/TR_Col-WEB.png);
        background-repeat: no-repeat;
        line-height: 52px;
    #CollapsiblePanel4 .CollapsiblePanelClosed .CollapsiblePanelTab  {
        background-color: #C3CFDF;
        border-radius: 5px 5px 0px 0px;
        color: #999
        text-shadow: 1px 1px #000;
        font-family: "Trebuchet MS", Arial, Helvetica, sans-serif;
        font-size: 18px;
        font-weight: bold;
        background-image: url(images/TR_Gray2-WEB.png);
        background-repeat: no-repeat;
        line-height: 52px;
    #CollapsiblePanel5 .CollapsiblePanelOpen .CollapsiblePanelTab {
        background-image: url(images/TR_Col-WEB.png);
        background-repeat: no-repeat;
        font-size: 18px;
        line-height: 52px;
        color: #FFF;
    #CollapsiblePanel5 .CollapsiblePanelTabHover .CollapsiblePanelTab {
        background-color: #003366;
        color: #FFF;
        text-shadow: 1px 1px #000;
        font-weight: bold;
        background-image: url(images/TR_Col-WEB.png);
        background-repeat: no-repeat;
        line-height: 52px;
    #CollapsiblePanel5 .CollapsiblePanelClosed .CollapsiblePanelTab  {
        background-color: #C3CFDF;
        border-radius: 5px 5px 0px 0px;
        color: #999
        text-shadow: 1px 1px #000;
        font-family: "Trebuchet MS", Arial, Helvetica, sans-serif;
        font-size: 18px;
        font-weight: bold;
        background-image: url(images/TR_Gray2-WEB.png);
        background-repeat: no-repeat;
        line-height: 52px;
    </style>

  • HT201342 How can I add an e-mail address to my existing icloud addresses?  I currently only use one and I would like to ad two more?

    How can I add an e-mail address to my existing icloud account?  I curently only use one of the three.

    Each iCloud account only has a single iCloud account.  If you want, you can add up to 3 alias addresses that will receive email in the same iCloud inbox, as explained here: http://support.apple.com/kb/PH2622.

  • How can I add amazon to Apple TV?

    I am a new Apple TV user. So far I am impressed and like it a lot. However I would like to use the Amazon Prime and I have yet to find it. How can I add it to my Apple TV?

    You cannot add apps to Apple TV. If you have an IOS device you can AirPlay to stream using the app from there

  • How can i add to a listBox items near in a new column other items ?

    The items on the right i changed the property of the listBox1 righttoleft to Yes.
    My problem is i want to build a new column for the number so each number i'm adding will be next on the left to the belong item string. And not under it like now.
    1. Maybe i need to use another control and not listBox ?
    2. How can i add column/s and also maybe adding a title to each column like: Names Id Number....?

    If you want columns, you should consider a
    ListView or
    DataGridView.

  • How can i add filename on a document in pages 5.1 ?

    I am allways using a template to make documents, that I save under different names. How can I add the (changed) filename in the footer of a saved document ?
    It is important to me to print the filename on document, to find and edit it on my mac later.
    In MS Word it is easy to automaticaly add filenames, path, author and other filedata to the document. I can't find how to do that automatically in Pages templates.
    I saw earlyer answers to this question on this community, but they don't work as I expect, or only on earlier versions of pages.....  I trust there is an easy way to do this.
    Can someone help me please ?

    That function, present in "Pages 09" (i.e. Pages 4.3) , was not included in Pages 5.0-5.1.  (Nor were nearly 100 other such features, from mail merge and bookmarks to non-contiguous text selection, 2-up page viewing and the ability to set alternating left/right margins, headers & footers.Version 5.1 is also notably buggy. One work around, or partial work around, is to click the file name at the top of the window document. You can then select the filename, copy it  and paste it whereever you like. As far as I can see, however, there is no way to include the path: all you get is the filename and extension.

  • Pls help - How can I add a typekit font to muse?

    As Muse doesn't come with all typekit fonts already included in the dropdown list of webfonts, I'd like to know how I can add a typekit font to the dropdown menu so I can use it for my website. I have Adobe creative cloud membership.
    I've searched the whole of the web and can't find anything about this at all - only about adding one of the existing muse typekit fonts which I already know how to do.
    Why doesn't Adobe include all typekit fonts with Muse when you're already a full creative cloud subscriber?

    Done!
    Date: Thu, 20 Dec 2012 03:13:17 -0700
    From: [email protected]
    To: [email protected]
    Subject: Pls help - How can I add a typekit font to muse?
        Re: Pls help - How can I add a typekit font to muse?
        created by morgan_in_london in Help with using Adobe Muse - View the full discussion
    You're very welcome - can I be cheeky and ask for a "correct answer" to be noted? :^D
         Please note that the Adobe Forums do not accept email attachments. If you want to embed a screen image in your message please visit the thread in the forum to embed the image at http://forums.adobe.com/message/4935814#4935814
         Replies to this message go to everyone subscribed to this thread, not directly to the person who posted the message. To post a reply, either reply to this email or visit the message page: http://forums.adobe.com/message/4935814#4935814
         To unsubscribe from this thread, please visit the message page at http://forums.adobe.com/message/4935814#4935814. In the Actions box on the right, click the Stop Email Notifications link.
         Start a new discussion in Help with using Adobe Muse by email or at Adobe Community
      For more information about maintaining your forum email notifications please go to http://forums.adobe.com/message/2936746#2936746.

  • HT201317 how can I add multiple photos to camera roll

    how can I add multiple photos to camera roll

    If you mean from photo stream, open your photo stream album on your phone in the thumbnail view, tap Edit, tap all the photos to select them, tap Share, then tap Save to Camera Roll.

  • How can i add  fields to a cube which is in production support

    hi,
       how can i add fiels to a cube which is in production support.

    Hi Maruthi
    You need to add new fields to cube in Development system and then transport it to production after testing.
    Edit cube in RSA1. Add the new infoobjects from right hand directory to left hand pane and assign these new infoobjects to suitable dimensions. Save and activate the infocube. Now go to change update rules for this infocube and do proper mapping of these infoobjects.
    Save & activate the update rules.
    RSA1-> Transport connection-> select cube, update rules and related objects and create transport request.
    After testing is done, give the transport request number to your Basis person and he will do the transports for you.
    Regards
    Pradip

  • How can we add fields to me80fn

    i want to add Material Type,work center,include delivery complete as check box to the open purchase order report ME80FN how can we add to this? what r the steps to do enhancement for this report?

    This is SAP Query report, look at infoset /SAPQUERY/ME80FN and you may copy this infoset to your own name space and then change it include your fields.
    You cannot modify the exisiting query

  • How can I add a field to a Repeating Frame in the body of a 9i Report

    How can I add a field to a Repeating Frame in the body of an Oracle 9i Report whose source is in a higher group?
    I want to change an Oracle 9i report. The report has no header or trailer but only a body. The body has the parent frame fields & a repeating frame with four fields, A, B, C & D. They all seem to be in one group. I want to add another field E to the repeating frame whose source is from the 'header'.
    I added the field & created a boiler plate & then added the new field, E to the original group A, B, C & D. In the Property Inspector for the field, I pointed the source to the source column in the main query. However, when I run the report, I receive the error "Invalid body size". Some notes on Metalink indicate that this could be due to margins being out of the printable area.
    Moreover, after making the changes & after having unsuccessfully run the report, when I try to open the Page Layout for the report, I get no details.
    Thanks,

    Hi,
    i also got same query can u pls tell me elaborately, if u can can u pls send some code.
    Thanks & Regards
    Jagadeeshwar.B

  • HT5538 I have a different phone number when traveling, that the one registered in my account. How can I add it?

    I have a different phone number when traveling, that the one registered in my account. How can I add it?

    Lost information
    https://www.adobe.com/account.html for serial numbers or subscriptions on your Adobe page... or
    Lost serial # http://helpx.adobe.com/x-productkb/global/find-serial-number.html

  • How can i add rows in a tabular form

    Hi,
    How can i add rows in a tabular form with out updating in database and after adding the rows one by one and after filling the data then iwant to submit them all at once.Please help me on this.
    Thanks

    Hello Leandro,
    In the Add_Rows page process, there is a box for "Number Of Rows". Change that value and you change the number of rows that get added. The default is 1.
    Don.
    You can reward this reply by marking it as either Helpful or Correct :)

  • How can i add my itunes library to my iphone?

    how can I add my itunes library to my iphone. when i sync no music is added?

    http://manuals.info.apple.com/en_US/iphone_user_guide.pdf

Maybe you are looking for