Simple C++ program to round given number half down.
#include <iostream> #include <cmath> using namespace std; // 2018 TheFlyingKeyboard and released under MIT License // theflyingkeyboard.net double roundHalfDown(double x, int n); int main() { double x = 0; int n = 0; cout << "Enter x: "; cin >> x; cout << "Enter n: "; cin >> n; cout << "roundHalfDown(" << x << ", " << n << ") is equal to " << roundHalfDown(x, n); int a; cin >> a; return 0; } double roundHalfDown(double x, int n) { return ceil(pow(10, n) * x - 0.5f) / pow(10, n); }
IN 20.5 0 -20.5 0 OUT 20 -21