Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 12-29-2008, 10:17 PM   #1 (permalink)
Registered Member
 
Join Date: Dec 2008
Posts: 45
moonraker928 is on a distinguished road
Default rounding float to 'x' decimal places

is there a method in objective-c that can take:

float f = 4.000000;

and make it so that it only has one decimal place:

float f = 4.0;

??
moonraker928 is offline   Reply With Quote
Old 12-29-2008, 10:40 PM   #2 (permalink)
Registered Member
 
RickMaddy's Avatar
 
Join Date: Oct 2008
Location: Denver, CO
Posts: 2,121
RickMaddy will become famous soon enough
Default

Is this for display purposes? There's no other reason to round a float otherwise.

To round it for display purposes you could do:

Code:
float f = 3.1415926;
NSString *str = [NSString stringWithFormat:@"%.1f", f];
where you can replace the '1' with however many decimal places you want to use.
RickMaddy is offline   Reply With Quote
Old 12-29-2008, 10:42 PM   #3 (permalink)
New Member
 
Join Date: Sep 2008
Posts: 88
nacht is on a distinguished road
Default

The float, or the *output* of the float?

If you are looking to keep the "float" down to that size--complete with rounding--then what you want is NSDecimalNumber. This will allow you to do fixed precision arithmetic.

If you want to change the output, that's a slightly easier problem: Use [NSString stringWithFormat:@"%.1f,f]. The .1f comes straight of printf string formatting guidelines and there's a String Formatting Guide you can find in your documentation.

Hope that helps.
nacht is offline   Reply With Quote
Old 08-02-2010, 08:58 AM   #4 (permalink)
www.Alexj17.com
 
Alexj17's Avatar
 
Join Date: May 2009
Location: Liverpool, England
Posts: 181
Alexj17 is on a distinguished road
Send a message via Skype™ to Alexj17
Default

Quote:
Originally Posted by RickMaddy View Post
Is this for display purposes? There's no other reason to round a float otherwise.

To round it for display purposes you could do:

Code:
float f = 3.1415926;
NSString *str = [NSString stringWithFormat:@"%.1f", f];
where you can replace the '1' with however many decimal places you want to use.
This worked perfect thank you !
Alexj17 is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 332
8 members and 324 guests
chemistry, Dnnake, iOS.Lover, lendo, leostc, Leslie80, pbart, VinceYuan
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,664
Threads: 94,120
Posts: 402,898
Top Poster: BrianSlick (7,990)
Welcome to our newest member, Leslie80
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 02:28 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0