向上或向下舍入 [英] Round up or down
问题描述
我有一个浮动,我想向上或向下舍入到最接近的整数。
I have a float which I'd like to round up or down to the nearest integer.
例如:
1.4 = 1.0
1.77 = 2.0
1.1 = 1.0
1.4 = 1.0 1.77 = 2.0 1.1 = 1.0
等......
我在Objective C中这样做,所以我想我需要一个标准的数学函数......附近会不会这样做?
I'm doing this in Objective C, so I guess I need a standard math function... Would nearbyintf do it?
推荐答案
您可以使用 math.h
中定义的任何标准C库数学函数。 nearbyintf
可以使用, roundf
或 ceilf
或 floorf
,具体取决于你想要的。
You can use any of the standard C library math functions defined in math.h
. nearbyintf
would work, as would roundf
or ceilf
or floorf
, depending on what you want.
Xcode将向你展示这些功能的文档(UNIX手册页)你可以选择 - 在编辑器中双击它们。
Xcode will show you the documentation (UNIX manual page) for these functions if you option-double-click them in the editor.
这篇关于向上或向下舍入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!