• 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: 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
_______________________________________________
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: Catching mouse-clicks outside UI elements
  • Next by Date: Re: MutableArrays losing its contents?
  • Previous by thread: Does an enumerator retain the last object?
  • Next by thread: Re: rounding a float
  • Index(es):
    • Date
    • Thread