I made this method
+ (CGFloat) round: (CGFloat)f {
int a = f;
CGFloat b = a;
return b;
}
It works as expected but it only rounds down. And if it's a negative number it still rounds down.
This was just a quick method I made, it isn't very important that it rounds correctly, I just made it to round the camera's x and y values for my game.
Is this method okay? Is it fast? Or is there a better solution?
You are reinventing the wheel - and this is a C question, not Objective C. Just use the standard C round() function.
2018 Answer
The other answers here are either dated or don't give good examples. It is easy to round a
CGFloat
using Swift's built inrounded
function.If you want to round the value in place you can use
round
:Rounding Rules
If you want more precise control over how numbers are rounded, you can use a
FloatingPointRoundingRule
.Away from zero
Numbers above zero are rounded up and numbers below zero are rounded down.
Down
Rounds any number with a decimal value down to the next smaller whole number. This is the same as
floor(x)
.To nearest or away from zero
Decimal numbers get rounded to the nearest integer value. However, when the value is exactly in the middle (like
3.5
or-3.5
) then positive numbers get rounded up and negative numbers get rounded down.It may have a long complicated name, but this is normally how one learns rounding in school. It is also the rule used if you just do
x.rounded()
.To nearest or even
This is similar to
toNearestOrAwayFromZero
, except now the.5
values get rounded to the even whole number.Toward zero
This just has the effect of cutting off any decimal values. If you needed an
Int
you could do the same thing withInt(x)
.Up
This is the opposite of
.down
. All decimal numbers are rounded up. This is the same asceil(x)
.Notes
round
androunded
are stillCGFloat
. If you need anInt
you have to convert it likeInt(myCGFloat)
.round(x)
,ceil(x)
andfloor(x)
anymore. However, if you do use them, they handle both 64 and 32 bit architecture so any answers you may have seen withroundf
,ceilf
andfloorf
are now obsolete.For working with 32 and 64 bit you can create your own macro's like
Ps this isn't an answer for the question but an addition for the question about how to work with 32/64 bit values.
Define this in a file like MyMacros.h and than add an import in the myapp-Prefix.pch
Also remember that choosing CGFloat as prefix for your function can be a risk since apple might add a macro like this them self so that is why the #ifndef CGFLOAT_CEIL is
A
CGFloat
is typedef'd to either adouble
or afloat
, so you can round them like any other real type:Note that while this works with floats small enough to carry a fraction, it may act weird on large values.
There are already standard functions with behaviors you might need in
<math.h>
such as:floorf
,ceilf
,roundf
,rintf
andnearbyintf
(lasf 'f' means "float" version, versions without it are "double" versions).It is better to use standard methods not only because they are standard, but because they work better in edge cases.
2013 Update (jessedc)
iOS is no longer only 32 bit. There are a number of other answers to this question that are now more relevant.
Most answers mention importing
tgmath.h
Try
#import "tgmath.h"
.The
<tgmath.h>
header will include the headers<math.h>
and<complex.h>
and will define several type-generic macros.