Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > Round to Nearest .5 in Cocoa Application

Round to Nearest .5 in Cocoa Application
Thread Tools
Fresh-Faced Recruit
Join Date: Jun 2006
Status: Offline
Reply With Quote
Apr 9, 2006, 02:00 AM
 
Please understand that I am working on my very first Cocoa application, so I certainly may just be missing something obvious.

In my application I have an NSTextField which displays the float value result of a calculation. I'd like to have the value displayed rounded to the nearest .5. For instance, 2.1 would display as 2 and 2.3 would display as 2.5. I tried the following, but it didn't work:

NSNumber * roundingIncrement = [NSNumber numberWithFloat: .5];
NSNumberFormatter * myFormatter = [[[NSNumberFormatter alloc] init] autorelease];

[myFormatter setRoundingIncrement: roundingIncrement];
// Insert other setters for minimum, maximum, format strings, etc.
[myTextField setFormatter: myFormatter];

Any help or insights will be appreciated. Thanks!
     
Senior User
Join Date: Feb 2003
Location: USA
Status: Offline
Reply With Quote
Apr 9, 2006, 08:03 AM
 
Normally I would turn to plain old C in a case like this, but rounding in C has always been ugly at best ( http://groups.google.com/group/comp....rch+this+group )

You could look into:

- (NSDecimalNumber *)decimalNumberByRoundingAccordingToBehaviorid <NSDecimalNumberBehaviors>)behavior

I'm sure others more familiar with floating point math will chime in here. Good luck!
MacBook 2.0 160/2GB/SuperDrive
Lots of older Macs
     
Mac Elite
Join Date: Mar 2001
Location: England
Status: Offline
Reply With Quote
Apr 9, 2006, 08:51 AM
 
double the number, add 0.5, take the integer part then half it?

2.1*2=4.2
4.2+0.5=4.7
int part = 4
halved = 2

2.3*2+0.5=5.1
int part = 5
halved = 2.5


Amorya
What the nerd community most often fail to realize is that all features aren't equal. A well implemented and well integrated feature in a convenient interface is worth way more than the same feature implemented crappy, or accessed through a annoying interface.
     
ipsych  (op)
Fresh-Faced Recruit
Join Date: Jun 2006
Status: Offline
Reply With Quote
Apr 9, 2006, 11:36 AM
 
Thanks, Amorya! That did the trick. I generalized it below to work to find the nearest .25, .5, .75, etc.

float decimalValue;
float decimalIncrement = .25; // Set to .25, .5, .75, etc.

decimalValue = decimalValue * (1/decimalIncrement);
decimalValue += decimalIncrement;
decimalValue = (int)decimalValue * decimalIncrement;
     
   
Thread Tools
Forum Links
Forum 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
Top
Privacy Policy
All times are GMT -5. The time now is 09:36 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2