example2_formatted_output.cpp

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
#include <iostream>
#include <iomanip>

int main() {
    double pi = 3.14159265358979;

    std::cout << "Default: " << pi << std::endl;
    std::cout << "Fixed: " << std::fixed << pi << std::endl;
    std::cout << "Scientific: " << std::scientific << pi << std::endl;
    std::cout << "Precision (3): " << std::setprecision(3) << pi << std::endl;
    std::cout << "Width (10): " << std::setw(10) << pi << std::endl;
    std::cout << "Left justified: " << std::left << std::setw(10) << pi << std::endl;

    return 0;
}
Back to iostream