Re: Rounding off numbers
Re: Rounding off numbers
- Subject: Re: Rounding off numbers
- From: John Stiles <email@hidden>
- Date: Sat, 10 Jun 2006 09:46:43 -0700
Floating point values will never be perfectly precise. It's likely that
the third number is really "2.964999997" or something like that. There
is nothing you can do to make floats "perfect," sorry :)
Johan Augustsson wrote:
Hi!
I have a problem when I round of numbers. My first question is if
there aren't any smother way to round off an NSNumber. I do it like this:
myNbr is an NSNumber.
NSNumber *myRoundedNbr=[NSNumber numberWithDouble:[[NSString
stringWithFormat:@"%.2f", [myNbr floatValue]] doubleValue]];
The bigger problem is that it doesn't round of correctly. Some tests
show this:
Nbr Rounded
2.265 => 2.27 OK
2.865 => 2.87 OK
2.965 => 2.96 WRONG
What can possibly be wrong and how do I solve it???
I use MacOS X 10.4.6
XCode 2.2
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden