ios - iPhone Calculator App Unwanted Decimal Places -


i made watched tutorial calculator iphone app. works having issue unwanted decimal places, meaning app put dot after number , have 6 0s after it. looked how fix , tried saw, works when type in first number in. when click multiplication, division, subtraction, or addition button reappear , when shows answer includes unwanted decimals too. code below, please me thanks!

viewcontroller.h

// //  viewcontroller.h //  simple calculator // //  created michael on 2013-08-08. //  copyright (c) 2013 com.company. 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 michael on 2013-08-08. //  copyright (c) 2013 com.company. 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];     // additional setup after loading view, typically nib. }  - (void)didreceivememorywarning {     [super didreceivememorywarning];     // dispose of resources can recreated. }  @end 

thanks :)

in buttonoperationpressed:sender method you're missing period in format string:

calculatorscreen.text = [nsstring stringwithformat:@"%2f",result]; 

should calculatorscreen.text = [nsstring stringwithformat:@"%.2f",result];

i.e. %.2f instead of %2f.

if want padding can use %2.2f instead.

here's tutorial on format specifiers.


Comments

Popular posts from this blog

css - Which browser returns the correct result for getBoundingClientRect of an SVG element? -

gcc - Calling fftR4() in c from assembly -

.htaccess - Matching full URL in RewriteCond -