How to convert from text to number integer? - iphone

i have input only number from text field and i want to change it to integer. what code should i write in?
just say text field say for 10 and i want to take that 10 = x, x = integer. so if i use it for mathematic process a = x + 1 and display say 11. is there any chance? i hope you understand what i mean. thank you. i think both of you will answer this simple question but i not because im beginner. im a 3d design graphic switch 180 degree become xcode programming. BTW ive tried this before but it cant.
NSInteger number = [[txtBtn text] integerValue];
Edited :
i've got this error, local declaration of 'number' hides instance variable, pointed to number at result = (number * 2) - 1; and i put the code like this.
NSInteger number = [[txtBtn text] intValue];
result = (number * 2) - 1;
i dont know whats wrong.

Are you sure it's a TextField and not a UIButton you are trying to get the integerValue from?
[txtBtn text]
I created a TextField and a UIButton, and implemented your code:
- (IBAction)btnClicked:(id)sender {
NSInteger i = [[inputField text] intValue];
int result = (i*2)-1;
NSLog(#"%i", result);
}
the output results is correct.
ex: type 5, output in console is 9.

Related

Working With 4 textfields and id sender 10 buttons

i`m trying to figure out what if i have 4 textfields field1,field2,field3 and field4, and 10 buttons tagged from 0 to 9, and when i press on a button it will show its tag number on the SELECTED textfield
my code for the 10 buttons is
- (IBAction)buttonPressed:(id)sender {
currentNumber = currentNumber *10 + (float)[sender tag];
field1.text = [NSString stringWithFormat:#"%0.f",currentNumber];
}
this will only send the tag to field1.
i have 4 fields and i want the button to send the tag to the selected field.
is it possible ? if its can you please explain it to me ?
thank you in advance
Have a global
UITextField * selectedField;
When a user selects the field
selectedField = field1
or
selectedField = field2
or
selectedField = field3
or
selectedField = field4
Then you could implement your
- (IBAction)buttonPressed:(id)sender {
currentNumber = [selectedField.text floatValue];
currentNumber += (float)[sender tag];
selectedField.text = [NSString stringWithFormat:#"%0.f",currentNumber];
}
If may be easier (and may hold the right number better) to manipulate your input as a string rather than converting it to the numeric , then adding value, then re-converting back to string though!!! I think you're going to find doing it the way you are, than you will run into floating point conversion problems going back and forth the way you are!!!
This way you are always setting the text to the selected field
i found it
it was very easy :)
if ([field1 isFirstResponder])
that would solve the problem :)

Implementing decimal instead of fraction, Calculator Xcode App

Ive scoured everywhere and haven't found a situation similar to mine.. New to Xcode/Objective C, I am putting together a dual purpose (fraction/decimal) calculator app. Ive found some good stuff on the fractions, and have them calculating correctly. However the decimal is a different story. I can get it to appear with this:
-(IBAction) pressDecimal
{
NSRange range = [display.text rangeOfString:#"."];
if ( range.location == NSNotFound ) {
display.text = [display.text stringByAppendingString:#"."];
}
or this:
-(IBAction) pressDecimal
{
[displayString appendString: #"."];
display.text = displayString;
}
The latter works great, to DISPLAY it on the calculator. I try to read it in as a slot, a double, and i just can't seem to read it in properly... here is how i get the digits in:
- (void) workNums: (int) nums
{
currentNumber = currentNumber * 10 + nums;
[displayString appendString:
[NSString stringWithFormat: #"%i", nums]];
display.text = displayString;
}
//uses tags from attribute inspector (decimal tagged 10)
-(IBAction)pressNum:(UIButton *)sendIT
{
int nums = sendIT.tag;
[self workNums: nums];
}
Ive looked everywhere, tried changing from floats to doubles ( i can get it to display 0.00's when i play with them) also here is my method to converting the ANSWER that is a FRACTION to a DECIMAL..which works great too:
- (double) convertToNum
{
if (denominator != 0)
return (double) numerator / denominator;
else
return NAN;
So again, cleanly stated... How may I get Objective C to see the decimal inputed in the app, and display the answer as a decimal properly.. am i screwed because i started this a mainly a fraction calculator? Thanks from a noob that moved from BASIC to Objective C
EDIT 4/27/12 ***
(new to Stack - but please read my comment below.. i am looking to find out why i can't read decimals into my program... I've tried changing from ints (which i know won't work) and doubles and floats, but all i get is 0.00 on user input when running this all as doubles:
-(IBAction)pressNum:(UIButton *)sendIT
{
int nums = sendIT.tag;
[self workNums: nums];
}
- (void) workNums: (int) nums
{
currentNumber = currentNumber * 10 + nums;
[displayString appendString:
[NSString stringWithFormat: #"%i", nums]];
display.text = displayString;
}
So the goal is to be able to CALCULATE a user inputted decimal number...any suggestions on a if/else situation as these numbers build up in an accumulator / and a double for currentNumber and get turned into numerators/denominators later down the line..(which is probably my issue, but it shouldn't be hard, but I'm making it hard, to use a BOOL or something to say, hey..this isn't a fraction, its a decimal - so lets do this - PLEASE HELP!! GOING NUTS!
I may be thinking this is much simpler than it is, but: if you already have a string like #"3.14" in displayString then you can convert that directly to a double:
double doubleValue = [displayString doubleValue];
Would that not be enough to get the double value and perform calculations?

Store user input, calculate and show in the end

I'm writing a program that calculates input a user gives, simple numbers. 2, 4, 7, 10, 12 and so on.
I need the program to store these inputs (that come in from different textfields) somehow. Use them in an internal equation, and then show them to the user in the end.
How do I do this? Please take note that I'm rather new at programming.
I know how to define textFields, do a calculate IBAction , and I've tried creating a -(void)calculate, which works, but only on 1 input. When I execute the following code, with input from 6 textfields instead of 3 (which are used in the first line (resultOne)) it gives me the wrong numbers. But I still need a better way of doing this.
-(void) calculate{
weightOne = [v1Text.text floatValue]/2;
weightTwo = [v2Text.text floatValue]/2;
resultOne = [m1Text.text floatValue] * weightOne + [s1Text.text floatValue] * weightOne;
resultTwo = [m2Text.text floatValue] * weightTwo + [s2Text.text floatValue] * weightTwo;
_resultTotal = (resultOne + resultTwo) / (weightOne + weightTwo);
NSString *resultString = [[NSString alloc]
initWithFormat: #"%.2f", resultOne];
resultLabel.text = resultString;
[resultString release];
}
I know that the above code outputs resultOne, but this is just for my own testing, to know that the equation works. But I would really like to start all fresh, since I don't believe that the above code is suitable for what I need it to do.
Please bare in mind that I am a beginner, and explain a little more, than just showing me some code.
I look forward to any help you can give me.
EDIT! - BELOW IS NEW CODE
-(IBAction) calculate:(id)sender {
weightOne = 1;
weightTwo = 0.75;
weightThree = 0.50;
mOne = [m1Text.text floatValue];
mTwo = [m2Text.text floatValue];
sOne = [s1Text.text floatValue];
sTwo = [s2Text.text floatValue];
resultOne = (mOne*weightOne) + (sOne*weightOne);
resultTwo = (mTwo*weightTwo) + (sTwo*weightTwo);
_resultTotal = (resultOne + resultTwo) / (weightOne*2 + weightTwo*2);
NSString *resultString = [[NSString alloc]
initWithFormat: #"Gennemsnit %.2f", _resultTotal];
resultLabel.text = resultString;
}
I decided to change my code, in order to easily calculate the input. Now I have another question.
Is it possible to save this equation in some way, so I can reuse it as many times as I want, without having to type the code again and again and again.
First of all calculate can be your IBAction method which will be called when tapped on the button. So change the prototype to -(IBAction)calculate:(id)sender.
Next if you are able to have outlets for the text fields, then there is no harm and absoulutely no difficulty in getting the text entered in them.
Few things you can do are to validate for correct input. [v1Text.text floatValue] is right expression for getting the numbers from text. You can also have a look at intvalue.
Now once you have done your computation, you can use the reference to the label/textfield where you want to display the result.
You can change that label exactly the way you did in your code.

How to subtract the int values within 2 UITextFields

I have a IBAction UILabel to add a number every time it is pressed. I have a Total UITextField that a adds all 3 UILabels. Getting the total seems to be fine until I hit the subtract(buttonTap2) UILabel button.
Here is an example of the IBAction UILabel code not working:
- (IBAction)buttonTap2:(id)sender {
int value = [currentLabel.text intValue] - 1;
currentLabel.text = [NSString stringWithFormat:#"%d",value];
int n1 = [label2.text intValue]; // total labels
int n2 = [label3.text intValue];
int n3 = [label4.text intValue];
int s = n1 - n2 - n3;
NSString *sn = [NSString stringWithFormat:#"%d",s];
[tex7 setText:sn];
What problem are you having? What is "tex7"? If it's supposed to be a textfield or label, is it correctly linked up in Interface Builder? You might have missed it as it isn't very descriptive.
In this case you only get negative values if n1n2>n3. If you want to convert negative numbers to positive number then you take mode of the number ,what ever the number is converted in to Positive number. Another thing you do is that compere the resultant number if it is less then 0 then multiple it with -1,

Display answer in UITextField?

Lets say I have:
int a = textbox1;
int b = textbox2;
answer = a + b;
How do you get "answer" to display in textbox3? I'm sure this is a very simple question, but believe it or not, I have done a lot of researching and found nothing. If you could please give an example as "Text" and as "numbers with decimals" I would greatly appreciate it!! Thank you in advance!
Use the text property of NSString to get and set it's text. Use intValue method of NSString to convert the string to integer (assuming that the string contains a valid integer). And finally use stringWithFormat to create a string which contains an integer.
Here is the one line code.
textBox3.text = [NSString stringWithFormat:#"%d", [textBox1.text intValue] + [textBox2.text intValue]];
a and b should probably be floats or doubles if you want decimal values. You can try something like this:
double a = [[textBox1 text] doubleValue];
double b = [[textBox2 text] doubleValue];
[textBox3 setText:[NSString stringWithFormat:#"%f", (a + b)]];