views:

86

answers:

4

I have strings that look about like this:

stringA = @"29.88";
stringB = @"2564";
stringC = @"12";
stringD = @"-2";

what is the best way to convert them so they can all be used in the same mathmatical formula?? that includes add, subtract.multiply,divide etc

+2  A: 

Probably floatValue (as it appears you want floating-point values), though integerValue may also be of use (both are instance methods of NSString).

Isaac
+2  A: 

[stringA doubleValue]

Marcelo Cantos
A: 

Assuming that you have NSString variables.

NSString *stringA = @"29.88";
NSString *stringB = @"2564";
NSString *stringC = @"12";
NSString *stringD = @"-2";

suppose, you want to convert a string value to float value, use following statement.

float x=[stringA floatValue];

suppose, you want to convert a string value to integer value, use following statement.

NSInteger y = [stringC intValue];

Hope, it helps to you.

sugar
+1  A: 

These are all wrong, because they don't handle errors well. You really want an NSNumberFormatter.

If you have the string @"abc" and try to use intValue or floatValue on it, you'll get 0.0, which is obviously incorrect. If you parse it with an NSNumberFormatter, you'll get nil, which is very easy to distinguish from an NSNumber (which is what would be returned if it was able to parse a number).

Dave DeLong