开发者

Comparing two double variables Objective-C

开发者 https://www.devze.com 2023-03-18 08:36 出处:网络
double a = 10.123420834; double b = 100.123412321; if (a > b) { // do something here } I am trying to compare the two values,开发者_如何学C the code above doesn\'t seems to work. Any idea?The co
double a = 10.123420834;
double b = 100.123412321;

if (a > b) {
// do something here
}

I am trying to compare the two values,开发者_如何学C the code above doesn't seems to work. Any idea?


The code is correct.

Note that your snippet is equivalent to

float a = 10.123420834;
float b = 100.123412321;

if (a > b) {
// do something here
}

since Objective C uses double by default unless the number is followed by an f.

Also note that a < b, so the if statement will always evaluate to FALSE. Hence you may want to do

double a = 10.123420834;
double b = 100.123412321;

if (a > b) {
// do something here
} else {
// do something else here
}

to test this properly.


double a = 10.123420834
double b = 100.123412321

You need to have a semicolon at the end of each of those lines.


The code in your example is correct. Your problem must be in the "do something here", or elsewhere.

0

精彩评论

暂无评论...
验证码 换一张
取 消