views:

84

answers:

2

i want to sum to values which i get from a textfiel. how can i cast a textfield value in double value ?

Regards Caglar

A: 

See this question: http://stackoverflow.com/questions/169925/how-to-do-string-conversions-in-objective-c

double myDouble = [myTextField.text doubleValue];
Jon Rodriguez
thank u. i checked your link. but they sum integer values. How can i sum double values ?
In your view controller, have a double property for the sum, like "@property (assign) double sum;". In your init method, set "self.sum = 0.0;". Then, to sum values as they are inputted, you would probably have a button that the user presses after entering each number. That button should trigger a method (via an IBAction if you're using a nib) that will do "self.sum = self.sum + [myTextField.text doubleValue];" and it should also clear the text field, like " myTextField.text = @""; "
Jon Rodriguez
And of course you can also reset "self.sum = 0.0;" whenever appropriate, such as when the user presses a "Reset" button
Jon Rodriguez
+2  A: 

You can use doubleValue to convert a NSString (from your text field) to a double like so:

double myDouble = [myTextField.text doubleValue]; 

This isn't a cast, by the way, it's a conversion. The doubleValue method performs a conversion from a string to a double representation. Casts are something you can only perform on primitive numeric types or pointers in Objective-C.

LBushkin