Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6620073
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T21:03:09+00:00 2026-05-25T21:03:09+00:00

I have a simple calculator app and I want it to be so that

  • 0

I have a simple calculator app and I want it to be so that if the answer requires no decimal places, there are none, just whole numbers. If the answer was 2, I don’t want it to say 2.000000, it should say 2. If it requires one decimal place, it should show to one decimal place for example 12.8 instead of 12.80. How would I do this? Here is my code.
btw, this is from a tutorial at http://www.youtube.com/watch?v=Ihw0cfNOrr4, not my own work.

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

in the .m

#import "calcViewController.h"

@implementation calcViewController



-(IBAction)buttonDigitPressed:(id)sender {


currentNumber = currentNumber *10 + (float)[sender tag];
calculatorScreen.text = [NSString stringWithFormat:@"%2f", currentNumber];

}

-(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];

}

-(IBAction)cancelInput {

currentNumber =0;
calculatorScreen.text = @"0";

}


-(IBAction)cancelOperation {



currentNumber = 0;
calculatorScreen.text = @"0";
currentOperation = 0;

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T21:03:10+00:00Added an answer on May 25, 2026 at 9:03 pm

    One way is to use NSNumberFormatter to format your result instead of NSString’s -stringWithFormat::

    NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
    [formatter setMaximumFractionDigits:requiredDigits];
    [formatter setMinimumFractionDigits:0];
    NSString *result = [formatter stringFromNumber:[NSNumber numberWithFloat:currentNumber];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say I want to have a simple web app that takes some user input,
I have a simple Flask app that contains a few basic views. One of
I have a simple calculator app. I have 4 operator buttons and the calculator
Imagine I write a simple calculator application, that just calculates simple stuff like 1.5
I'm familiar with core data but want to make a simple app that calculates
I have coded my calculator in simple mode, I have added a button for
I have a SQL statement (MS SQL Server 2005) that does a simple calculation
i'm using Moscow ML combined with CGI. I have a site that calculates simple
I have simple php validation form that is halfway working. If you leave the
I have simple win service, that executes few tasks periodically. How should I pass

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.