开发者

Showing distance property in a table cell rounded to two decimal places, and showing a suffix of Km

I have the following code showing location objects from an NSArray in a table, with each index of the NSArray represented by a cell in the table in my SecondViewController.m class:

    - (UITableViewCell *)tableView开发者_如何学C:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"restaurantcell";

    LocationTableViewCell *cell = (LocationTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
 cell = [[[LocationTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }

 Location *location = [locationList objectAtIndex:indexPath.row];
 cell.locationName.text = location.name; 
 cell.locationAddress.text = location.address;
 cell.locationDistance.text = location.distance;

    return cell;
}

My relevant code in my LocationTableViewCell.m class looks like this:

UILabel *rDistance = [[UILabel alloc] initWithFrame:CGRectMake(250, 0, 250, 20)];

lDistance.font = [UIFont systemFontOfSize:13]; lDistance.numberOfLines = 0; [self.contentView addSubview:lDistance]; locationDistance = lDistance; [lDistance release];

At the moment, I am getting the distance property appearing with many decimal places. The distance property is in Km, so I want it to be rounded to two decimal places, and with the suffix "Km" appended to the label with a space between the distance property, and the "Km". How would I do this?


Check the following code

float distance = 120.354686;
NSString distanceString = [NSString stringWithFormat:@"%0.2f Km",distance];
UILabel *rDistance = [[UILabel alloc] initWithFrame:CGRectMake(250, 0, 250, 20)];
rDistance.text = distanceString;
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜