John
John

Reputation: 63

Convert Math equation into string

![HERE is what my log is showing][1]I am working on a calculator and I have a function that takes in user input and then will display the answer just lie a normal calculator. I want to save any user input into a string such as "2+2=4" into an array which can then be viewed later.

Is there any way to convert the double values into strings? and also how would i save this string in the array.

Below is some code which i have tried, but have had no luck. The below method is used to call the users input

  NSString *leftString = [NSString stringWithFormat:@"%d + %d", left, right]; 
    _array = [[NSArray alloc] initWithObjects: leftString, nil];       

Upvotes: 0

Views: 115

Answers (2)

JustAnotherCoder
JustAnotherCoder

Reputation: 2575

Assuming you want to have an array like so: [left, operation symbol, right] then you do the following:

NSString *leftString = [NSString stringWithFormat:@"%f", left];
NSString *rightString = [NSString stringWithFormat:@"%f", right];
NSString *operationString= [NSString stringWithFormat:@"%@", operation];

Then do the following to add all of them into an array:

_array = [[NSArray alloc] initWithObjects: leftString, operation, rightString, nil];

Upvotes: 0

Lyndsey Scott
Lyndsey Scott

Reputation: 37300

This:

NSString *leftString = [NSString stringWithFormat:@"%d", left "+" right];

should be like this:

NSString *leftString = [NSString stringWithFormat:@"%f + %f", left, right];

where the plus sign is within the expression and the %f indicates left and right are doubles.

Upvotes: 1

Related Questions