fork download
  1. #include <iostream>
  2.  
  3. #include <iomanip>
  4.  
  5. using namespace std;
  6.  
  7. int main() {
  8.  
  9. // initialize a floating-point
  10.  
  11. float num = 2.71828;
  12.  
  13. cout << "Original number is: " << num;
  14.  
  15. cout << "\n";
  16.  
  17. // print 2 decimal places
  18.  
  19. cout << "The number with 2 decimal places is: ";
  20.  
  21. cout << fixed << setprecision(2) << num;
  22.  
  23. cout << "\n\n";
  24.  
  25. return 0;
  26.  
  27. }
Success #stdin #stdout 0s 5276KB
stdin
Standard input is empty
stdout
Original number is: 2.71828
The number with 3 decimal places is: 2.718