setw

Formatting Output

Sometimes floating-point numbers need to be output in a specific way. For example, cash denominations are printed to two decimal places, whereas scientific measurements might require output to ten or more decimal places. Likewise, we often want to output tabular data in fixed width columns.

We can use the setprecision manipulator to control the output of floating-point numbers. To print floating-point output to two decimal place, we use the setprecision manipulator to 2.  In order to use the setprecision manipulator, we must include the header file iomanip.

#include <iostream>
#include <iomanip>


int main(void){
    
    double billA = 123.80;
    double billB = 20.21;
    
    std::cout << '$' << billA << std::endl;
    std::cout << '$' << billB << std::endl << std::endl;
    
    std::cout << '$' << std::fixed << std::setprecision(2) << billA << std::endl;
    std::cout << '$' <<  billB << std::endl;
    
    return 0;
    
}

The setprecision manipulator changes the output format of decimal numbers until a similar subsequent statement changes the precision.

The manipulator scientific is used to output floating-point numbers in scientific format.

#include <iostream>
#include <iomanip>

using namespace std;

int main(void){
    
    double x = 1.61803;
    double y = 25.806975;
    double z = 2.718;
    
    cout << "Using scientific notation:" << endl;
    cout << scientific << x << endl << y << endl << z << endl;
    
    
    cout << endl << "Using fixed notation:" << endl;
    cout << fixed << x << endl << y << endl << z << endl;
    
    
    cout << endl << "Setting precision to 3:" << endl;
    cout << setprecision(3) << x << endl << y << endl << z << endl;
    
    return 0;
    
}

We can also use the showpoint manipulator to show the decimal point and trailing zeros of floating point numbers that have a decimal part that is zero.

#include <iomanip>

using namespace std;

int main(void){

double a, b, c;

a = 192.9864;
b = 168.0;
c = 3.14159;

cout << fixed << showpoint;

cout << setprecision(2) << "Precision set to 2:" << endl;
cout << a << endl;
cout << b << endl;
cout << c << endl;

cout << setprecision(4) << "Precision set to 4:" << endl;
cout << a << endl;
cout << b << endl;
cout << c << endl;

cout << setprecision(6) << "Precision set to 6:" << endl;
cout << a << endl;
cout << b << endl;
cout << c << endl;

return 0;

}

We use the manipulator setw to output the value of an expression in specific columns. The value of this expression can be either a string or a number. The output is right-justified. Unlike setprecision, which controls the output of all floating-point numbers until otherwise specified, setw only controls the output of the next expression.

#include <iostream>
#include <iomanip>

using namespace std;

int main(void){

    double x = 255.2549;
    int y = 567;
    int z = 73;
    
    
    cout << fixed << showpoint;
    
    cout << setw(7) << y << endl;
    cout << setw(7) << z << endl << endl;
    
    cout << setprecision(2);
    cout << setw(8) << x << setw(8) << y << setw(8) << z;
    cout << endl;
    
    return 0;
    
}

By default, the columns set by the setw manipulator are right-justified. To left-justify the output, we can use the manipulator left. To set the output back to right-justified, we use the manipulator right.