How to round CGFloat

后端 未结 6 1382
佛祖请我去吃肉
佛祖请我去吃肉 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 18:57

    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.

提交回复
热议问题