iomanip

iomanip is a header file that is part of the standard C++ library. iomanip defines manipulator functions that can be used to affect the state of iostream objects.

Let’s take pi to 10 decimal places, 3.1415926535. We will store this in a variable, piTest and manipulate its precision using a method named setprecision(). With setprecision() we can alter the precision of a floating-point number.

#include <iostream>
#include <iomanip>
using namespace std;

int main() 
{
double piTest = 3.1415926535;

    cout << "\n" << "     " << "piTest = " << piTest << "\n\n";
    cout << "     " << "With setprecision set to 4dp, piTest = " << setprecision(4) << piTest << "\n\n";
    cout << "     " << "With setprecision set to 6dp, piTest = " << setprecision(6) << piTest;
}