• 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 a float
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: rounding a float


  • Subject: Re: rounding a float
  • From: Public Look <email@hidden>
  • Date: Mon, 1 Dec 2003 22:27:54 -0500

If you want decimal precision, use NSDecimalNumber.
If you want floating point precision and can deal with the fact that many "real" numbers can not be accurately represented with the float format, use existing floating point functions like round(), floor() ceil(), etc. For example, traditional rounding is to two decimal places often accomplished with floor((number * 100.0f) + 0.5f)/100.0f;

Or you could use any of the wonderful printf formatting strings to get the output you want without changing the number that is stored at all.

Finally, did you know that whole college courses on numerical methods are available ? Wonderful discussions about the nature of numbers stored in finite/discrete amounts of memory and number systems and the fact that many non-repeating decimals are infinitely repeating series in binary, etc. are available. It is all fascinating stuff :) Look up "Binary Coded Decimal" sometime.



On Dec 1, 2003, at 7:48 PM, Jay Rimalrick wrote:

ok say I wanted to divide 1.23 by 2

i would really behind the scenes I would take 123/2 which would give me 61.5
and which is 61 in integer form ... I lose the .5 which i need it to round up to
62.

I display it as (123/2)/100 which is .61 .... I lose that cent :-(

please correct my understanding above and let me know if I am still not
getting it.

Thanks

---------- Original Message ----------------------------------
From: Rainer Brockerhoff <email@hidden>
Date: Mon, 1 Dec 2003 14:01:14 -0200

At 02:39 -0500 01/12/2003, Jay Rimalrick wrote:
I don't know how well the integers will work either ... consider this scenario

long int test = (100/51);
NSLog(@"%i", test);

the actual answer is 1.9607...
the expected integer would be 2
however the nslog output is 1

it does not seem that "This will round things automatically" even with
integers
.... am I not understanding something completely?

Nope. What I was suggesting is, make all calculations in cents (multiply all
values by 100 internally). Then fake the decimal point when printing.

So, to adapt your example:
long int test = (10000/51);
NSLog(@"%ld.ld\n",test/100,test0);
will print out 1.96, and
long int test = 196+54+123456+1; // instead of
1.96+0.54+1234.56+0.01;
NSLog(@"%ld.ld\n",test/100,test0);
will print out 1237.07.

As I said, that's how we did it before floating point became generally
available.

--
Rainer Brockerhoff <email@hidden>
Belo Horizonte, Brazil
"It's extremely unlucky to be superstitious, for no other reason
than it is always unlucky to be colossally stupid." (Stephen Fry)
Weblog: http://www.brockerhoff.net/bb/viewtopic.php





________________________________________________________________
Sent via the WebMail system at 1st.net
_______________________________________________
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.

  • Prev by Date: Re: MutableArrays losing its contents?
  • Next by Date: Re: NSBezierPath
  • Previous by thread: Re: rounding a float
  • Next by thread: Re: rounding a float
  • Index(es):
    • Date
    • Thread