17yrsDev
17yrsDev

Reputation: 215

Multiply data from a UItextfield?

I'm new to coding and i need help for something very simple. I want to create a Youtube View/Money calculator so i need to take the number of views and multiply it by 0.4 ish to get a rougly estimate of how much that video made according to the views. My main.storyboard looks like this: I have a UItextField (which is supposed to get the number of views, a calculate button (which does the 0.4 multplication) and a label to display the result.

Here is my viewcontroller.h code:

int NumberOfViews;

@interface ViewController : UIViewController
{
    IBOutlet UILabel *result;
}

- (IBAction)Calculate:(id)sender;

@property (weak, nonatomic) IBOutlet UITextField *NumberOfViews;

And now here's my viewcontroller.m code:

-(IBAction)Calculate:(id)sender{

     NumberOfViews = NumberOfViews * 0.4;
     result.text = [NSString stringWithFormat:@"Money made %i", NumberOfViews];

}

Im pretty sure it's because the Number of views from the UITextField doesnt link to the 0.4 multiplication aka the calculate method since the results is always 0 but how can i link those?? It's the most simple thing but cant figure it out! I want to take a number from UITextField and multiply it by 0.4 and display the results... That's it. A little bit of help would be greatly appreciated.. Thanks!

Upvotes: 1

Views: 356

Answers (2)

JiaYow
JiaYow

Reputation: 5227

UPDATE 2 Seems that @0x7fffffff thought he misread, his answer was correct, you're multiplying your (global - that's bad!) integer with a float and %i prints a integer. @0x7fffffff please repost your answer :-P

UPDATE: Argh, I just saw that you have two variables called "NumberOfViews". See, that's why I originally asked you to check the coding style guide ;-) @0x7fffffff has the correct answer for you.

Original Post

You are multiplying a textfield with a number.

Do something like:

float numViews = [NumberOfViews.text intValue] * 0.4
result.text = [NSString stringWithFormat:@"Money made %f", numViews];

But you need to make sure that the text in the textfield is actually a number first.

Also, please check the coding style guide under https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/CodingGuidelines/CodingGuidelines.html, "NumberOfViews" is a bad name for a text field.

Upvotes: 2

Mick MacCallum
Mick MacCallum

Reputation: 130193

You're multiplying an int by a double and then storing the result back in an int. Since ints can't store decimals, rounding occurs. To fix this, you should change your declaration of numberOfViews to use the float type, or the double type.

float NumberOfViews;

Then, you'll need to change the format specifier you use in your formatted string to be %f for floating point numbers.

result.text = [NSString stringWithFormat:@"Money made %f", numberOfViews];

Additionally, you should name your instances to start with lower case letters to help distinguish them from classes.

Upvotes: 1

Related Questions