我需要对一个数字进行四舍五入,但我不知道该数字是负数还是正数。
是否有更好的方法来舍入 foo
来做到这一点:
static_cast<int>(foo > 0 ? foo + 0.5 : foo - 0.5)
基本上我想要这种行为:
3.4 => 3
3.5 => 4
-3.4 => -3
-3.5 => -4
最佳答案
对于 C++,有一个:std::lround
关于c++ - 四舍五入正数或负数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31162992/