How to convert type double to string iPhone?

user762034 picture user762034 · Jul 14, 2011 · Viewed 15.1k times · Source

I am calculating the velocity of an iPhone, and I need to know how to convert the variable calculatedSpeed which is type double to type string to display on a label.

Here is what I have in the header:

@interface myProject : UIViewController <CLLocationManagerDelegate> {

double calculatedSpeed; 
    UILabel *velocityLabel;
}

And here is what I have in the main:

-(void)speedLocation:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
double gpsSpeed = newLocation.speed;

if(oldLocation != nil)
{
    CLLocationDistance distanceChange = [newLocation getDistanceFrom:oldLocation];
    NSTimeInterval sinceLastUpdate = [newLocation.timestamp timeIntervalSinceDate:oldLocation.timestamp];
    calculatedSpeed = distanceChange / sinceLastUpdate;

    velocityLabel.text = calculatedSpeed;

}

}

Notice how I am trying to set velocityLabel to calculatedSpeed, which is a variable with the type double. So it naturally gives me the error: incompatible type for argument 1 of 'setText:'.

Your help is greatly appreciated.

Thanks!

Answer

Dietrich Epp picture Dietrich Epp · Jul 14, 2011
velocityLabel.text = [NSString stringWithFormat:@"%g", calculatedSpeed];