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.

About these ads

§ One Response to Objective-C Math converting float to integer or string

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

What’s this?

You are currently reading Objective-C Math converting float to integer or string at Web Builders.

meta

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: