在 Objective-C 中,如果大于或等于 .5,则四舍五入
Rounding up double, if greater or equal to .5, in Objective-C
如何舍入 Objective-C 中的以下值(它们是双精度值):
1.1 -> 1.0
1.6 -> 2.0
1.5 -> 2.0
2.3 -> 2.0
2.0 -> 2.0
2.001 -> 2.0
我在区分小于和大于或等于 .5
的各种值时遇到问题。
发件人:Objective-C Float Rounding
Use the C standard function family round(). roundf() for float,
round() for double, and roundl() for long double. You can then cast
the result to the integer type of your choice.
要对变量进行四舍五入,您通常可以加上 0.5,然后将结果转换为 int
(如果您愿意,还可以转换为 float
或 double
),或者调用floor()
截断小数部分的函数。
如何舍入 Objective-C 中的以下值(它们是双精度值):
1.1 -> 1.0
1.6 -> 2.0
1.5 -> 2.0
2.3 -> 2.0
2.0 -> 2.0
2.001 -> 2.0
我在区分小于和大于或等于 .5
的各种值时遇到问题。
发件人:Objective-C Float Rounding
Use the C standard function family round(). roundf() for float, round() for double, and roundl() for long double. You can then cast the result to the integer type of your choice.
要对变量进行四舍五入,您通常可以加上 0.5,然后将结果转换为 int
(如果您愿意,还可以转换为 float
或 double
),或者调用floor()
截断小数部分的函数。