Re: Rounding to two decimals
Re: Rounding to two decimals
- Subject: Re: Rounding to two decimals
- From: Dave Rosborough <email@hidden>
- Date: Sat, 17 Jul 2004 22:58:08 -0700
A common problem with floating point arithmetic on computers. Try
searching Google for 'floating point precision problems'.
Two options - use a number formatter in IB on the field, or rather than
using setFloatValue, try something like:
[averageLabel setStringValue:[NSString stringWithFormat:@"%.2g",
average]];
TTYL
DaveR
On 17-Jul-04, at 3:18 PM, Ken Garden wrote:
Probably it's just a slight mistake. Here's the relevant code snippet
with some sample data filled for the integers:
[BEGIN]
int total, multiply;
float average;
total = 5;
multiply = 8;
average = (float)multiply / total;
average = round(average * 100) / 100;
[averageLabel setFloatValue:average];
[END]
The text printed in averageLabel is "1.6000000238...". It seems the
rounding is done quite nicely, I'm just wondering why there's such a
strange extension. An idea how I can get rid of this?
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.