example2_precision_fixed_point.cpp

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <iomanip>
#include <cmath>

int main() {
    double pi = M_PI;
    double small = 0.0000123;
    double large = 123456789.0;

    std::cout << "Default output:" << std::endl;
    std::cout << pi << std::endl << small << std::endl << large << std::endl;

    std::cout << "\nFixed, 2 decimal places:" << std::endl;
    std::cout << std::fixed << std::setprecision(2);
    std::cout << pi << std::endl << small << std::endl << large << std::endl;

    std::cout << "\nScientific, 3 significant digits:" << std::endl;
    std::cout << std::scientific << std::setprecision(3);
    std::cout << pi << std::endl << small << std::endl << large << std::endl;

    return 0;
}
Back to iomanip