ios 如何舍入 CGFloat
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2667130/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to round CGFloat
提问by Johannes Jensen
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.
这只是我制作的一个快速方法,它是否正确舍入并不是很重要,我只是为我的游戏将相机的 x 和 y 值舍入。
Is this method okay? Is it fast? Or is there a better solution?
这个方法行吗?速度快吗?或者有更好的解决方案吗?
回答by Suragch
2018 Answer
2018 答案
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.
这里的其他答案要么过时,要么没有给出很好的例子。CGFloat
使用 Swift 的内置rounded
函数可以很容易地对 a进行四舍五入。
let x: CGFloat = 3.5
let y = x.rounded() // 4.0
If you want to round the value in place you can use round
:
如果您想将值四舍五入到位,您可以使用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
.
如果您想更精确地控制数字的舍入方式,可以使用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)
.
将具有十进制值的任何数字向下舍入为下一个较小的整数。这与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.
十进制数四舍五入到最接近的整数值。但是,当值正好在中间(如3.5
或-3.5
)时,正数会向上舍入,负数会向下舍入。
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()
.
它可能有一个很长很复杂的名字,但这通常是人们在学校学习四舍五入的方式。如果你只是这样做,这也是使用的规则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.
这类似于toNearestOrAwayFromZero
,但现在.5
值四舍五入为偶数整数。
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)
.
这只是具有切断任何十进制值的效果。如果你需要一个,Int
你可以用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)
.
这与.down
. 所有十进制数都四舍五入。这与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
androunded
are stillCGFloat
. If you need anInt
you have to convert it likeInt(myCGFloat)
. - There is no need to use the C math functions
round(x)
,ceil(x)
andfloor(x)
anymore. However, if you do use them, they handle both 64 and 32 bit architecture so any answers you may have seen withroundf
,ceilf
andfloorf
are now obsolete.
- 不要忘记考虑负值。
- 结果
round
和rounded
仍然CGFloat
。如果你需要一个,Int
你必须像Int(myCGFloat)
. - 有没有必要使用C数学函数
round(x)
,ceil(x)
和floor(x)
了。但是,如果你使用他们,他们同时处理64位和32位架构,所以你可能已经看到有任何答案roundf
,ceilf
而floorf
现在已经过时了。
回答by SergGr
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).
目前已经与行为,你可能需要在标准功能<math.h>
,如:floorf
,ceilf
,
roundf
,rintf
和nearbyintf
(lasf“F”表示“浮动”的版本,版本没有它是“双重”的版本)。
It is better to use standard methods not only because they are standard, but because they work better in edge cases.
最好使用标准方法,不仅因为它们是标准的,而且因为它们在边缘情况下效果更好。
2013 Update (jessedc)
2013 更新 (jessdc)
iOS is no longer only 32 bit. There are a number of other answers to this question that are now more relevant.
iOS 不再只有 32 位。这个问题还有许多其他答案现在更相关。
Most answers mention importing tgmath.h
大多数答案提到导入 tgmath.h
回答by zneak
A CGFloat
is typedef'd to either a double
or a float
, so you can round them like any other real type:
ACGFloat
被 typedef 为 adouble
或 a float
,因此您可以像任何其他真实类型一样对它们进行舍入:
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.
请注意,虽然这适用于小到足以携带分数的浮点数,但对于大值可能会表现得很奇怪。
回答by Mikhail
Try #import "tgmath.h"
.
试试#import "tgmath.h"
。
The <tgmath.h>
header will include the headers <math.h>
and <complex.h>
and will define several type-generic macros.
该<tgmath.h>
头将包括头部<math.h>
和<complex.h>
并规定若干类型的通用宏。
回答by Saren Inden
For working with 32 and 64 bit you can create your own macro's like
要使用 32 位和 64 位,您可以创建自己的宏,例如
#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.
Ps 这不是问题的答案,而是关于如何使用 32/64 位值的问题的补充。
Define this in a file like MyMacros.h and than add an import in the myapp-Prefix.pch
在 MyMacros.h 之类的文件中定义它,然后在 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
还要记住,选择 CGFloat 作为函数的前缀可能会有风险,因为苹果可能会自己添加这样的宏,这就是为什么 #ifndef CGFLOAT_CEIL 是
回答by Paul Lynch
You are reinventing the wheel - and this is a C question, not Objective C. Just use the standard C round() function.
您正在重新发明轮子 - 这是一个 C 问题,而不是目标 C。只需使用标准的 C round() 函数即可。