上下取整 C++实现
2种方法:1、使用库函数。2、自己实现相应函数。
贴一个自己的实现:
#include <iostream> using namespace std; int roundingDown(double num); int roundingUp(double num); int main(void) { double a; cin >> a; cout << roundingDown(a) << endl; cout << roundingUp(a) << endl; return 0; } int roundingDown(double num) { int n = static_cast<int>(num); if (n == num) { return n; } if (num > 0) { return n; } if (num < 0) { return n - 1; } } int roundingUp(double num) { int n = static_cast<int>(num); if (n == num) { return n; } if (num > 0) { return n + 1; } if (num < 0) { return n; } }