• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Rounding to two decimals
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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.


References: 
 >Rounding to two decimals (From: Ken Garden <email@hidden>)
 >Re: Rounding to two decimals (From: Yann Bizeul <email@hidden>)
 >Re: Rounding to two decimals (From: Ken Garden <email@hidden>)

  • Prev by Date: Re: readFromURL:url options: not working for me
  • Next by Date: Re: Rounding to two decimals
  • Previous by thread: Re: Rounding to two decimals
  • Next by thread: Re: Rounding to two decimals
  • Index(es):
    • Date
    • Thread