formated string std::cout
Code Example:
#include <iostream>
#include <iomanip>
int main() {
double value = 3.14159;
std::cout << std::fixed << std::setprecision(2) << value << std::endl;
return 0;
}
Explanation:
- Include Headers:
- The
#include <iostream>
directive includes the input/output stream library. The
#include <iomanip>
directive includes the input/output manipulator library.Main Function:
The
main
function is the entry point of the program.Variable Declaration:
A variable
value
of typedouble
is declared and initialized with the value3.14159
.Output Formatting:
std::fixed
sets the floating-point numeric output to fixed-point notation.std::setprecision(2)
sets the precision of the floating-point output to 2 decimal places.Output Statement:
std::cout << ... << value << std::endl;
outputs the formatted value to the standard output stream followed by a newline.Return Statement:
return 0;
indicates successful program termination.