std::setprecision

Header: <iomanip>

When used in an expression out « setprecision(n) or in » setprecision(n), sets the precision parameter of the stream out or in to exactly n.

# Declarations

/*unspecified*/ setprecision( int n );

# Parameters

# Return value

An object of unspecified type such that

# Example

#include <iomanip>
#include <iostream>
#include <limits>
#include <numbers>
 
int main()
{
    constexpr long double pi{std::numbers::pi_v<long double>};
 
    const auto default_precision{std::cout.precision()};
    constexpr auto max_precision{std::numeric_limits<long double>::digits10 + 1}; 
 
    std::cout << "default precision: " << default_precision << '\n'
              << "maximum precision: " << max_precision << "\n\n"
                 "precision: pi:\n";
 
    for (int p{0}; p <= max_precision; ++p)
        std::cout << std::setw(2) << p << "  " << std::setprecision(p) << pi << '\n';
 
    std::cout << std::setprecision(default_precision); // restore defaults
}

# Defect reports

DRApplied toBehavior as publishedCorrect behavior
LWG 183C++98setprecision could only be used with streamsof type std::ostream or std::istreamusable with anycharacter stream

# See also