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

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.
References: 
 >Re: Round a float (From: Steve Bird <email@hidden>)

  • Prev by Date: Re: cocoa-dev digest, Vol 2 #2711 - 14 msgs
  • Next by Date: Re: [ANN] irregular shaped buttons class
  • Previous by thread: Re: Round a float
  • Next by thread: RE: Round a float
  • Index(es):
    • Date
    • Thread