views:

180

answers:

1

I have very little programming knowledge; only a fair bit in Visual Basic.

How do I take a value from a text field, then do some simple math such as divide the value by two, then display it back to the user in the same field?

In Visual Basic you could just do txtBoxOne.text = txtBoxOne.text / 2

I understand this question is more than one question and is very basic stuff, but I need to get my head out of Visual Basic and into where I should be :)

+2  A: 

You can use doubleValue on NSString objects to retrieve the numeric value from a string.

There are variations on this, such as intValue, floatValue, boolValue, etc.

I suggest that you stick with the primitive type that has the longest range (double).

For example:

NSString *str1 = @"15";
NSString *str2 = @"5";

double result = [ str1 doubleValue ] / [ str2 doubleValue ];

NSString *str_result = [ NSString stringWithFormat: @"%f", result ];
Jacob Relkin
Nice answer, that has also answered a few other things as well.
David Maitland