-4

int Bonus = .3 (need percentage this equals 30%)

label = (textfield * Bonus)

New to Objective-C, standing by if more clarifications is needed.

update: SOLVED! Solution:

HERE IS .h file:

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController {

 IBOutlet UILabel *label;
 IBOutlet UITextField *textfield;
}


- (IBAction)save:(id)sender;

@end

Here is .m file

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController





- (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.
}

- (IBAction)save:(id) sender {

    float x = ([textfield.text floatValue]);
    float bonus = .3;
    float result;
    result = x * bonus / 10; 

// Divide by 10 to keep it percentage based




    label.text = [[NSString alloc] initWithFormat:@"%.2f", result];

Thanks for everyones help! Especially Xu Yin!!

Ouch
  • 3
  • 3

2 Answers2

1

I agree with pacman321, maybe try to add some more information next time..

looks like your questions is:

//1.> "convert NSString to number" 
float value = [textfield.text floatValue];

//2.> do the math 
value = value * Bonus; //Could also be value *= Bonus;

//3.> Convert number back to NSString
label.text = [NSString stringWithFormat:@"%f", value];

I think that's all you need ,but some suggestions here: make sure your textfield will only have numerical value. and may need to pay attention to how much digits you want to display in the label.

Xu Yin
  • 3,932
  • 1
  • 26
  • 46
  • FYI - the 2nd line can simply be `value *= Bonus;`. – rmaddy Feb 07 '14 at 01:31
  • @rmaddy you are right, mine maybe slightly easier for him to understand, but i will add yours to my answer. Thanks! – Xu Yin Feb 07 '14 at 01:35
  • thanks, can you look at my update i still have issues? – Ouch Feb 07 '14 at 02:47
  • @Ouch you use int instead of float for you bonus, int will ignore the decimal places, so your 0.3 --> 0 in this case. – Xu Yin Feb 07 '14 at 03:30
  • yea i got that figured out :) thanks. I have a question though, im not seeing it actually show the total result? do i need to specify total some how? – Ouch Feb 07 '14 at 03:53
  • for some reason the label.text = [NSString stringWithFormat:@"%f", value]; comes up with an error when trying to run. – Ouch Feb 07 '14 at 18:58
  • I tried what someone else recommended as well, but for some reason i cant get it to display..i posted the code under the last UPDATE: – Ouch Feb 07 '14 at 19:01
  • here is the error: @autoreleasepool { return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class])); } – Ouch Feb 07 '14 at 19:05
  • maybe you can set an breakout at all the exceptions ,and see where the code stops, i am pretty sure the code i sent you won't cause this problem.. – Xu Yin Feb 07 '14 at 19:11
  • libc++abi.dylib: terminating with uncaught exception of type NSException – Ouch Feb 07 '14 at 19:23
  • for that, i can't help you with the code you paste, it seems fine for me. i did a quick search, maybe you can take a look at this post http://stackoverflow.com/questions/7855627/app-crashes-while-loading-with-error-in-main-m – Xu Yin Feb 07 '14 at 19:26
  • Maybe too much chat here, can you start another question and paste both your .h file code and .m file? – Xu Yin Feb 07 '14 at 20:21
  • @Xu Yin! Thanks for all your help! i was missing key information in my .h file that was vital! – Ouch Feb 07 '14 at 21:50
0

It's all explained there https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/Strings/Articles/FormatStrings.html

textfield.text = [NSString stringWithFormat:@"%d%%", Bonus * 100]
jbouaziz
  • 1,484
  • 1
  • 12
  • 24
  • 2
    This is not what the OP wants. – rmaddy Feb 07 '14 at 01:32
  • [Answers containing only links are rarely good answers.](http://meta.stackexchange.com/questions/8231/are-answers-that-just-contain-links-elsewhere-really-good-answers) Links rot, for one thing, and for another, people may not understand the link and a second explanation may help. – kojiro Feb 08 '14 at 20:24