How to round CGFloat

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

    There are already standard functions with behaviors you might need in 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

    • https://stackoverflow.com/a/5352779/184130
    • https://stackoverflow.com/a/7695011/184130

提交回复
热议问题