0

How would you? I'm having problems. Thanks.

I'm currently using

if (myString == myfloat) {
// do something but this won't work
}

OR

if ([myString == myFloat]) {
// do something but this won't work
}

Thanks!

1 Answer 1

6

The == operator only makes sense when the two types are compatible, which is not the case between strings and floats. One side must be explicitly converted to the other.

You may convert the NSString into a float:

if ([myString floatValue] == myFloat) {
  // Note: Use "fabs(a - b) < epsilon" to avoid inequality due to precision lost.

or convert the float into an NSString (very unusual):

if ([myString isEqualToString:[NSString stringWithFormat:@"%g", myFloat]]) {
  // Note: make sure the string is encoded as "%g" as well.
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.