How to round CGFloat

后端 未结 6 1377
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-05 18:21

I made this method

+ (CGFloat) round: (CGFloat)f {
    int a = f;
    CGFloat b = a;
    return b;
}

It works as expected but it only round

6条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-05 19:02

    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

提交回复
热议问题