开发者

Objective C: Comparison issue between numbers

I am trying to implement the code snippet below. However I am unable to get my expected results. For example if my disanceFromLoc = 0.16, I am still getting my output as 0.16 km.

Is there something wrong with the way I am comparing the value? object is of '开发者_运维百科NSNumber' class (defined it as double in core data)

if ([object.distanceFromLoc doubleValue] < 0)
    {
        cell.detailTextLabel.text = [[NSString alloc] initWithFormat:@"Approx. Distance: %0.2f m", [object.distanceFromLoc doubleValue]*1000];
    }
    else 
    {
        cell.detailTextLabel.text = [[NSString alloc] initWithFormat:@"Approx. Distance: %0.2f km", [object.distanceFromLoc doubleValue]];
    }

Thanks!

Zhen Hoe


I believe you mean to compare to one, not zero.

As it stands, you're checking if your distanceFromLoc is negative, which (presumably) never happens with a regular distance. Instead, you should be checking if you're closer than 1km (i.e. < 1), at which point you can multiply by 1000 and convert to meters.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜