开发者

distanceFromLocation to string is way off [duplicate]

This question already has answers here: Limit a double to two decimal places [duplicate] (5 answers) Closed 2 years ago.

So I am trying to take distanceFromLocation and show it as "8 miles" or "1.5 miles" but I end up with "93129432 miles". See comments inline for where it goes wrong.

 double roun开发者_运维问答dDouble = round(returnedDouble);
    NSNumber *mylngDoubleNumber = [NSNumber numberWithDouble:roundDouble];

    if(latDouble == 0 || lngDouble == 0 || roundDouble == 0){
        NSString *str = [NSString stringWithFormat:@"0 mi."];
        self.distance.text = str;
    }else {
        NSString *lngDoubleString = [mylngDoubleNumber stringValue]; <-- ok here '98'
        NSString *str = [NSString stringWithFormat:@"%d mi.",lngDoubleString]; <-- bad here '93129432'

        self.distance.text = str; 
    }


it should be [NSString stringWithFormat:@"%@ mi", lngDoubleString]; because you are formatting a string.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜