fork(1) download
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <cmath>
  4.  
  5. // fast pow for int, credit to https://stackoverflow.com/a/101613/13188071
  6. int ipow(int base, int exp)
  7. {
  8. int result = 1;
  9. while (true)
  10. {
  11. if (exp & 1)
  12. result *= base;
  13. exp >>= 1;
  14. if (exp == 0)
  15. break;
  16. base *= base;
  17. }
  18.  
  19. return result;
  20. }
  21.  
  22. double round_prec(double n, int prec)
  23. {
  24. return std::round(n * ipow(10, prec)) / ipow(10, prec);
  25. }
  26.  
  27. int main()
  28. {
  29. std::cout << std::setprecision(20) << round_prec(3.1415, 2) << '\n';
  30. }
Success #stdin #stdout 0s 5576KB
stdin
Standard input is empty
stdout
3.1400000000000001243