std cout 2 digits float

To output a floating-point number with two digits after the decimal point using the std::cout statement in C++, you can use the std::fixed and std::setprecision manipulators from the <iomanip> library.

Here are the steps to achieve this:

  1. Include the <iostream> and <iomanip> libraries at the beginning of your program:
#include <iostream>
#include <iomanip>
  1. Inside your code, use the std::fixed manipulator to set the floating-point output format to fixed-point notation:
std::cout << std::fixed;
  1. Use the std::setprecision manipulator to set the precision of the floating-point number to two decimal places:
std::cout << std::setprecision(2);
  1. Finally, use the std::cout statement to output the floating-point number:
float number = 3.14159;
std::cout << number << std::endl;

This will output the value of number with two digits after the decimal point, like this: 3.14.

Remember to include the necessary headers and apply the manipulators before using std::cout to output the floating-point number.