How to round CGFloat
Asked Answered
P

6

23

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?

Phago answered 19/4, 2010 at 11:57 Comment(0)
A
28

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

Aurelie answered 19/4, 2010 at 12:10 Comment(3)
Ah, thank you, Assuming the iPhone is 32-bit it'd be best to use those float functions. :DPhago
Anyone knows a macro to allow coding for 32-bit and 64-bit CPUs?Haematoxylon
Just create your own macro, see my comment somewhere else in this pageSewer
T
71

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 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

Rounding Rules

If you want more precise control over how numbers are rounded, you can use a FloatingPointRoundingRule.

Away from zero

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

Down

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

To nearest or away from zero

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

To nearest or even

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   ***

Toward zero

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

Up

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

Notes

  • Don't forget to take negative values into account.
  • The results of round and rounded are still CGFloat. If you need an Int you have to convert it like Int(myCGFloat).
  • There is no need to use the C math functions 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.
Tallie answered 14/1, 2016 at 8:47 Comment(2)
Great example of a great answer on SO. Thank you.Pizarro
This would be a great answer, except for the fact that the question is tagged objective-c ...Osana
A
28

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

Aurelie answered 19/4, 2010 at 12:10 Comment(3)
Ah, thank you, Assuming the iPhone is 32-bit it'd be best to use those float functions. :DPhago
Anyone knows a macro to allow coding for 32-bit and 64-bit CPUs?Haematoxylon
Just create your own macro, see my comment somewhere else in this pageSewer
D
5

A CGFloat is typedef'd to either a double or a float, so you can round them like any other real type:

CGFloat round(CGFloat aFloat)
{
    return (int)(aFloat + 0.5);
}

Note that while this works with floats small enough to carry a fraction, it may act weird on large values.

Dessert answered 19/4, 2010 at 12:2 Comment(0)
S
1

You are reinventing the wheel - and this is a C question, not Objective C. Just use the standard C round() function.

Silicone answered 19/4, 2010 at 12:10 Comment(4)
I am curious why this got two downvotes on the same day - especially as it is an exactly correct answer, just not as expansive as the accepted one.Silicone
Well round() is for doubles and roundf() is for floats. CGFloat is on 32-bit systems a float and on 64-bit systems a doubleUnific
Still doesn't explain the down votes - remember that this was three years ago!Silicone
Might be due to the theoretical possibility of a CGFloat being redefined to doubleUnific
F
1

Try #import "tgmath.h".

The <tgmath.h> header will include the headers <math.h> and <complex.h> and will define several type-generic macros.

Fitment answered 7/6, 2013 at 14:12 Comment(0)
S
0

For working with 32 and 64 bit you can create your own macro's like

#ifndef CGFLOAT_CEIL
    #ifdef CGFLOAT_IS_DOUBLE
        #define CGFLOAT_CEIL(value) ceil(value)
    #else
        #define CGFLOAT_CEIL(value) ceilf(value)
    #endif
#endif

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

Sewer answered 1/9, 2014 at 9:18 Comment(3)
I wouldn't re-invent the wheel here. tgmath.h ("type generic" math), as mentioned in other answers, already provides good macros for dealing with architecturally-different types.Vapor
This is just an example that can be applied for multiple solutions. Perhaps you do not agree on this example but can you tell me how NSUserDefaults i.e. can store an CGFloat? It has functions to set an float or double. Just like NSNumber does not have an function to retrieve an CGFloat. There are a lot of ways that this might be useful. And of course i do agree that you should not reinvent the wheel.Sewer
tgmath is overkill when all most people need is the right variant of round/floor/ceil. I also don't like that it redefines the standard functions, rather then using new names. I prefer this simple macro approach.Enyo

© 2022 - 2024 — McMap. All rights reserved.