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

 

C++ Round Half Down
Tagged on:         

Leave a Reply

Your email address will not be published. Required fields are marked *

This website stores some user agent data. These data are used to provide a more personalized experience and to track your whereabouts around our website in compliance with the European General Data Protection Regulation. If you decide to opt-out of any future tracking, a cookie will be set up in your browser to remember this choice for one year. I Agree, Deny
504

By continuing to use the site, you agree to the use of cookies. You can read more about it the Cookies&Privacy Policy Section Above. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this. You can read more about it the Cookies&Privacy Policy Section.

Close