## Objective-C Math converting float to integer or string

April 12, 2009 § 1 Comment

CLLocation by default report distance in meter. You can convert meter to feet by multiplying with 3.280839895. There is no round method to round off float. Adding 0.5 will give you similar result.

Convert a float into integer by casting the float in integer.

int myInteger = (int)(myFloat * 3.280839895 + 0.5);

OR you can assign fixed decimal places on a float before converting the float into string

float f = 3.280839895;

NSString *str = [NSString stringWithFormat:@"%.2f", f];

The number after “%.” will determine no. of fixed decimal places you would like to show on the float. Then stringWithFormat: method will display the float into a string.

Thank you! Great post! Really helpful!