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?
Swift provide a built-in function named as ceil() function. This function is used to round the given number to the nearest smallest integer value which is greater than or equal to the given number. It accept also both Float and Double.
The basic type for floating-point scalar values in Core Graphics and related frameworks.
The other answers here are either dated or don't give good examples. It is easy to round a CGFloat
using Swift's built in rounded
function.
let x: CGFloat = 3.5 let y = x.rounded() // 4.0
If you want to round the value in place you can use round
:
var x: CGFloat = 3.5 x.round() // 4.0
If you want more precise control over how numbers are rounded, you can use a FloatingPointRoundingRule
.
x.rounded(.awayFromZero)
Numbers above zero are rounded up and numbers below zero are rounded down.
3.000 -> 3.0 3.001 -> 4.0 3.499 -> 4.0 3.500 -> 4.0 3.999 -> 4.0 -3.000 -> -3.0 -3.001 -> -4.0 -3.499 -> -4.0 -3.500 -> -4.0 -3.999 -> -4.0
x.rounded(.down)
Rounds any number with a decimal value down to the next smaller whole number. This is the same as floor(x)
.
3.000 -> 3.0 3.001 -> 3.0 3.499 -> 3.0 3.500 -> 3.0 3.999 -> 3.0 -3.000 -> -3.0 -3.001 -> -4.0 -3.499 -> -4.0 -3.500 -> -4.0 -3.999 -> -4.0
x.rounded(.toNearestOrAwayFromZero) // same as x.rounded()
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()
.
3.000 -> 3.0 3.001 -> 3.0 3.499 -> 3.0 3.500 -> 4.0 *** 3.999 -> 4.0 -3.000 -> -3.0 -3.001 -> -3.0 -3.499 -> -3.0 -3.500 -> -4.0 *** -3.999 -> -4.0
x.rounded(.toNearestOrEven)
This is similar to toNearestOrAwayFromZero
, except now the .5
values get rounded to the even whole number.
3.000 -> 3.0 3.001 -> 3.0 3.499 -> 3.0 3.500 -> 4.0 *** 3.999 -> 4.0 4.500 -> 4.0 *** -3.000 -> -3.0 -3.001 -> -3.0 -3.499 -> -3.0 -3.500 -> -4.0 *** -3.999 -> -4.0 -4.500 -> -4.0 ***
x.rounded(.towardZero)
This just has the effect of cutting off any decimal values. If you needed an Int
you could do the same thing with Int(x)
.
3.000 -> 3.0 3.001 -> 3.0 3.499 -> 3.0 3.500 -> 3.0 3.999 -> 3.0 -3.000 -> -3.0 -3.001 -> -3.0 -3.499 -> -3.0 -3.500 -> -3.0 -3.999 -> -3.0
x.rounded(.up)
This is the opposite of .down
. All decimal numbers are rounded up. This is the same as ceil(x)
.
3.000 -> 3.0 3.001 -> 4.0 3.499 -> 4.0 3.500 -> 4.0 3.999 -> 4.0 -3.000 -> -3.0 -3.001 -> -3.0 -3.499 -> -3.0 -3.500 -> -3.0 -3.999 -> -3.0
round
and rounded
are still CGFloat
. If you need an Int
you have to convert it like Int(myCGFloat)
.round(x)
, ceil(x)
and floor(x)
anymore. However, if you do use them, they handle both 64 and 32 bit architecture so any answers you may have seen with roundf
, ceilf
and floorf
are now obsolete.There are already standard functions with behaviors you might need in <math.h>
such as: floorf
, ceilf
, roundf
, rintf
and nearbyintf
(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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With