fork download
  1. #include <cmath>
  2. #include <iomanip>
  3. #include <iostream>
  4. #include <limits>
  5.  
  6. long double my_round(long double value, unsigned decimal_places)
  7. {
  8. long double mult = std::pow(10.0L, decimal_places);
  9. return std::round(value * mult) / mult;
  10. }
  11.  
  12. int main()
  13. {
  14. long double Fred = 9.56756785685685682595L;
  15. auto max_prec = std::numeric_limits<long double>::digits10;
  16.  
  17. for (unsigned i = 0; i < max_prec; ++i)
  18. {
  19. std::cout << "decimal places: " << i << '\n';
  20. std::cout << std::setprecision(max_prec) << Fred << '\n';
  21. std::cout << std::setprecision(i+2) << my_round(Fred, i) << "\n\n";
  22. }
  23. }
Success #stdin #stdout 0s 3412KB
stdin
Standard input is empty
stdout
decimal places: 0
9.56756785685685683
10

decimal places: 1
9.56756785685685683
9.6

decimal places: 2
9.56756785685685683
9.57

decimal places: 3
9.56756785685685683
9.568

decimal places: 4
9.56756785685685683
9.5676

decimal places: 5
9.56756785685685683
9.56757

decimal places: 6
9.56756785685685683
9.567568

decimal places: 7
9.56756785685685683
9.5675679

decimal places: 8
9.56756785685685683
9.56756786

decimal places: 9
9.56756785685685683
9.567567857

decimal places: 10
9.56756785685685683
9.5675678569

decimal places: 11
9.56756785685685683
9.56756785686

decimal places: 12
9.56756785685685683
9.567567856857

decimal places: 13
9.56756785685685683
9.5675678568569

decimal places: 14
9.56756785685685683
9.56756785685686

decimal places: 15
9.56756785685685683
9.567567856856857

decimal places: 16
9.56756785685685683
9.5675678568568568

decimal places: 17
9.56756785685685683
9.56756785685685683