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 08-23-2011, 12:16 PM   #1 (permalink)
Registered Member
 
Join Date: May 2011
Posts: 15
xxpokerxx is on a distinguished road
Default Number rounding

Hi, is it possible to specify rounding to a certain position.

Basically i need to :

given this float what is it (hundreds, thousands, tens of thousands, millions, etc.) and then round up to the nearest whole number in that position.

So,

given 17,029 round to 18,000
given 1,348 round to 2,000
given 1,564,783 round to 2,000,000
given 443,756 round to 500,000

if it is possible, could anyone point me in the right direction. Thanks.
xxpokerxx is offline   Reply With Quote
Old 08-23-2011, 12:25 PM   #2 (permalink)
Registered Member
 
Join Date: Aug 2011
Posts: 25
rcirka is on a distinguished road
Default

You may want to try NSNumberFormatter. It gives various options for rounding. I haven't tried it for non-decimal rounding but it may work for you.
__________________
www.RonaldCirka.com
Equinox |TeamLiquid
Available for freelancing opportunities
rcirka is offline   Reply With Quote
Old 08-23-2011, 01:19 PM   #3 (permalink)
Registered Member
 
Join Date: Nov 2008
Posts: 864
nobre84 is on a distinguished road
Default

A pretty simple idea would be , keep dividing your number by 10 and place in a variable the total amount divided by, until your make the number < 10. Then, round up this number and multiply it by the total amount you divided earlier.

edit: this algorithm will round 17029 to 20000. The others will work as you said.

Last edited by nobre84; 08-23-2011 at 01:44 PM.
nobre84 is offline   Reply With Quote
Old 08-23-2011, 02:50 PM   #4 (permalink)
Registered Member
 
Join Date: May 2011
Posts: 15
xxpokerxx is on a distinguished road
Default

Thanks for the responses, i got it. I actually changed what i wanted a little bit, but its the same idea. instead of just rounding up the largeest digit, it rounds the second largest (the values are for use in a graph so i only need a small gap between the rounds. If I chose 1,100,000 with my previous request i would get 2,000,000 which is a huge difference and not good, but now i will get 1,200,000 which works out perfectly.

My solution, not pretty but it works:

Code:
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init] autorelease];
[formatter setRoundingMode:NSNumberFormatterRoundCeiling];

float digitToRound = pow(10, (double)[f stringFromNumber:[NSNumberWithFloat:floatToRound] length] - 2]);

return [[formatter stringFromNumber:[NSNumber numberWithFloat:(floatToRound / digitToRound)]] floatValue] * digitToRound;
this stops rounding when floatToRound is double digits and returns NAN when floatToRound is single digits. however for my purposes, those cases will never occur so i dont care.

Last edited by xxpokerxx; 08-23-2011 at 02:52 PM.
xxpokerxx is offline   Reply With Quote
Reply

Bookmarks

Tags
math, rounding

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: 399
14 members and 385 guests
7twenty7, chiataytuday, cristofercolmbos, dedeys78, fiftysixty, gmarro, iOS.Lover, jonathandeknudt, Matrix23, raymng, stanny, tymex, UMAD, xerohuang
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,669
Threads: 94,121
Posts: 402,903
Top Poster: BrianSlick (7,990)
Welcome to our newest member, dedeys78
Powered by vBadvanced CMPS v3.1.0

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