开发者

How do I Getting a TextField to do simple math?

开发者 https://www.devze.com 2023-03-22 17:27 出处:网络
I have three UITextFields. Two of them represent a certain number value. The third represents the percentage of the two. How do I setup the 3rd UITextField to开发者_StackOverflow do this simple math?Y

I have three UITextFields. Two of them represent a certain number value. The third represents the percentage of the two. How do I setup the 3rd UITextField to开发者_StackOverflow do this simple math?


You can simply get the intValue or the floatValue or the doubleValue of the text that you have received from the first two text fields. Eg:

float firstFloat = [self.firstTextField.text floatValue];
float secondFloat = [self.secondTextField.text floatValue];
float answer = firstFloat / secondFloat; //or whatever math you need to do

self.thirdTextField.text = [NSString stringWithFormat:@"%.2f",answer];

Hope this helps.


Check out "Getting Numeric Values" here:

http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/Reference/NSString.html

So you can use that to convert from the text property of your two UITextField instances.

Then you can convert them back to an NSString and plug them into the text of the third UITextField by using initWithFormat (something like [initWithFormat:@"%d", theResult]).


You can also do it in the following way.

NSString *str1,*str2;
str1=text1.text;
str2=text2.text;
int num1=[str1 intValue];
int num2 =[str2 intValue];
int ans=num+num2;
text3.text=[NSString stringWithFormat:@"%d",ans];

Hope this helps.

[answer setStringValue:[NSString stringWithFormat:@"%2.02g", answerFloat]];
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号