Re: rounding a float
Re: rounding a float
- Subject: Re: rounding a float
- From: "Mike T. Miller" <email@hidden>
- Date: Mon, 01 Dec 2003 18:12:38 -0800
I missed the beginning of this thread, but if the main concern is
rounding behavior and you are concerned about accuracy dealing with
cents, why not use NSDecimalNumber? It supports various rounding
behaviors and uses a decimal representation under the covers.
-mike
On Dec 1, 2003, at 5:51 PM, Kyle Moffett wrote:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
On Dec 01, 2003, at 19:55, Rainer Brockerhoff wrote:
At 19:48 -0500 01/12/2003, 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.
Correct. If you want to round up do: ((123+1)/2)/100. The extra 1 is
half of what you're dividing it by (in this case 2).
Or store it as tenths of a cent:
long long money = 1230; /* = $1.230 */
To divide:
long long newmoney = money/divisor
newmoney = 10*( (newmoney+5)/10 );
Or this :-)
typedef long long money_t;
#define MONEY(x) ((x) * 1000)
static inline M_MULT (money_t x, double y) {
return (10*(( x*y + 5 )/10));
}
Then:
money_t my_bank_account = MONEY(10000.00);
money_t my_desired_bank_account = M_MULT(my_bank_account, 1000);
money_t my_post_college_bank_account = M_MULT(my_bank_account, 0.0001);
Cheers,
Kyle Moffett
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.3 (Darwin)
iD8DBQE/y/Aoag7LSGnFq10RAs19AKCpIVT7BpA5+YI0K+u9qNNHibG0AwCfQsWg
DIpi0uFpsthTGHZ93x+Wb+o=
=QR1l
-----END PGP SIGNATURE-----
_______________________________________________
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.
--
Mike T. Miller
Sr. Java Architect, NorCal Software Services
Sun Microsystems, Inc.
_______________________________________________
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.