How to subtract the int values within 2 UITextFields - iphone

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,

Related

Concatenating Two Integers [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 9 years ago.
It might be the simplest Question, but at this time ,I am not getting any Idea on how to implement this.
Ok, The problem is of how to concatenate two integers.
For Eg: I want to create an integer say 0000 using two different integers 00 and 00. I tried using NSString , but I failed.
My Code is :
int num1 = 00;
int num2 = 00;
NSString *str = [NSString stringWithFormat:#"%d%d",num1,num2];
int num = [str intValue];
NSLog(#"num = %d",num); // It Logs 0 but I want 0000.
Does anyone have better Idea ?
EDIT :
Please Note that , I want to use that num to set the tag of textfield. That's why all the zeros are essential. So my main Problem starts here.
I have one tableview which contains custom cells. This custom cell has more than 10 textfields. Now I want to uniquely identify all the textfields for editing. That's why the tag for that textfield must be the integer concatenated by two values called rowNumber and textFieldNumber (means which textField out of 10.).
So my question is what I am trying to do is right or not ? And if not then give me some useful solution.
The integer data types (such as int) only store the integer value, not formatting information. Therefore you lose the number of leading zeroes (which do not affect the integer value, i.e., 0, 00, and 0000 are the same integer: zero).
If you wish to retain formatting information, you must store it separately. A simple way is to just store the string itself. Or, if you always want to have the same number of digits, then alter the formatting string:
int num1 = 0;
int num2 = 0;
NSString *str = [NSString stringWithFormat:#"%02d%02d",num1,num2];
After the above code, str will be "0000". However, converting it to int and then logging with %d formatting results in 0 once more (since 0000 and 0 are the same integer).
Edit: For the purpose of generating unique integers for tagging purposes, given a row number (rowNumber) and text field number (textFieldNumber), use a formula like:
tagNumber = rowNumber * 100 + textFieldNumber;
This way the text fields of row 0 will have numbers 0..99, those on row 1 will have 100..199, etc. If more than 100 text fields are required per row, simply multiply by a larger number, like 1000.
In integer arithmetic these values can be converted back to row and field numbers with:
rowNumber = tagNumber / 100;
textFieldNumber = tagNumber % 100;
Try to use this:
int num1 = 0;
int num2 = 0;
int num3=0;
int num4=0;
NSString *str = [NSString stringWithFormat:#"%d%d%d%d",num1,num2,num2,num3];
int num = [str intValue];
NSLog(#"num = %0*d",str.length,num);
Hope this helps you.
but dont init as like this "int num1=00" because it init "0" only in num1.
Why turn it back into an int? You should print the string you just formatted:
NSLog(#"num = %#",str);
As numbers, there is no difference between 0 and 0000. Integers only preserve the value, not the formatting (that's what strings are for).
Multiply the first integer with 100 and add the second integer to it. You should be able to print it using:
NSLog([NSString stringWithFormat:#"%04d", sum]);
which will put leading zeros.
You can pad it as :
NOTE: both the num1 and num2 must be of 2 digits. If its size increases then it wont work.
int num1 = 00;
int num2 = 00;
NSString *str = [NSString stringWithFormat:#"%d%d",num1,num2];
int num = [str intValue];
NSLog(#"num = %04d",num);
If printing is not the real issue then i guess you need a multiplication factor to set the tag
let it be 10000 and add it to some sequence no to get the unique tag value.
or you can use num as string and multiply by 10 in for loop for num.length times

Divide NSInteger with int

I am trying to divide NSInteger with some number, but I am receiving errors.
This is what I am trying to do:
length = [corner count];
for (int i=0; i<length; i++) {
if ([resultDate rangeOfString:[corner objectAtIndex:i]].location != NSNotFound) {
cornerResult++;
}
}
cornerResultLabel.text = [NSString stringWithFormat:#"%i", cornerResult];
This works as it should.. I search through the array and count the results and write it.
But, I need cornerResult divided with 4. When I add cornerResult / 4 it shows me the error(as I wrote in comment). I have no idea why is this making problem.
you probably meant to divide the intValue of the NSNumber, not the NSNumber* itself:
int num = number.intValue;
int result = num / 4;
(full code sample and error message would help)
You are dividing an int which becomes a float , use %f instead.
I fix it this way. First I defined int and after that I set NSInteger value to int and after that divide.
int helper = cornerResult;
helper = helper / 4;

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 :)

How to convert from text to number integer?

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.

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?