Simple C++ program to round given number half up.
#include <iostream> #include <cmath> using namespace std; // 2018 TheFlyingKeyboard and released under MIT License // theflyingkeyboard.net double roundHalfUp(double x, int n); int main() { double x = 0; int n = 0; cout << "Enter x: "; cin >> x; cout << "Enter n: "; cin >> n; cout << "roundHalfUp(" << x << ", " << n << ") is equal to " << roundHalfUp(x, n); int a; cin >> a; return 0; } double roundHalfUp(double x, int n) { return floor(pow(10, n) * x + 0.5f) / pow(10, n); }
IN 23.54 1 23.55 1 OUT 23.5 23.6
C++ Round Half Up