Re: rounding a float
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.