fork download
  1. #include <iostream>
  2. #include <cmath>
  3. using namespace std;
  4.  
  5. template<typename T>
  6. T round(T value, int number){
  7. int b = 10;
  8. T p = pow(double(b), number);
  9. T a = value * p;
  10. a = (abs(a)-abs(int(a)) < 0.5) ? int(a) : (int(a) + (a<0 ? -1 : 1));
  11. return a/p;
  12. }
  13.  
  14. int main() {
  15. // your code goes here
  16. cout << round(2.7, 0) << endl;
  17. cout << round(35.765, -1) << endl;
  18. return 0;
  19. }
Success #stdin #stdout 0s 4532KB
stdin
Standard input is empty
stdout
3
40