Re: Round a float
Re: Round a float
- Subject: Re: Round a float
- From: Dustin Voss <email@hidden>
- Date: Mon, 28 Jul 2003 18:04:43 -0700
On Monday, July 28, 2003, at 10:41 AM, Steve Bird wrote:
On Monday, July 28, 2003, at 09:41 AM, Lorenzo wrote:
Hi,
I have a
float floatSize = 105.55791;
and I would like to print it as @"105.5";
--- Everybody has said to use %0.1f, but notice that that function
will ROUND the number you gave UP to "105.6". If you really DO want
to see "105.5", you'll have to do the truncation yourself.
The rounding functions that return floating-point values are:
ceil -- towards positive infinity
floor -- towards negative infinity
trunc -- towards zero
nearbyint -- towards the nearest integer
Unfortunately, these only round to integers. To round 10.357 to 10.35,
you'd have to multiply 10.357 times 100.0, truncate, and divide the
result by 100.0. This can theoretically introduce inaccuracies -- you
might get 10.34999999 -- but I don't think that's likely so long as you
don't try for too many decimal positions.
_______________________________________________
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.