Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/io/manip/setprecision"

From cppreference.com
< cpp‎ | io‎ | manip
m (Update links.)
m (headers sorted, fmt)
 
(8 intermediate revisions by 7 users not shown)
Line 1: Line 1:
 
{{cpp/title|setprecision}}
 
{{cpp/title|setprecision}}
 
{{cpp/io/manip/navbar}}
 
{{cpp/io/manip/navbar}}
{{ddcl | header=iomanip |
+
{{ddcl|header=iomanip|
 
/*unspecified*/ setprecision( int n );
 
/*unspecified*/ setprecision( int n );
 
}}
 
}}
  
When used in an expression {{c|out << setprecision(n)}} or {{c|in >> setprecision(n)}}, sets the {{tt|precision}} parameter of the stream {{tt|out}} or {{tt|in}} to exactly {{tt|n}}.  
+
When used in an expression {{c|out << setprecision(n)}} or {{c|in >> setprecision(n)}}, sets the {{tt|precision}} parameter of the stream {{c|out}} or {{c|in}} to exactly {{c|n}}.  
  
 
===Parameters===
 
===Parameters===
 
{{par begin}}
 
{{par begin}}
{{par | n | new value for precision}}
+
{{par|n|new value for precision}}
 
{{par end}}
 
{{par end}}
  
 
===Return value===
 
===Return value===
 
+
{{cpp/io/manip/iomanip return value|setprecision}}
Returns an object of unspecified type such that if {{tt|str}} is the name of an output stream of type {{c|std::basic_ostream<CharT, Traits>}} or an input stream of type {{c|std::basic_istream<CharT, Traits>}}, then the expression {{c|str << setprecision(n)}} or {{c|str >> setprecision(n)}} behaves as if the following code was executed:
+
 
+
{{c|1=
+
str.precision(n);
+
}}
+
  
 
===Example===
 
===Example===
 
{{example
 
{{example
|
+
|code=
| code=
+
#include <iostream>
+
 
#include <iomanip>
 
#include <iomanip>
#include <cmath>
+
#include <iostream>
 
#include <limits>
 
#include <limits>
 +
#include <numbers>
 +
 
int main()
 
int main()
 
{
 
{
     const long double pi = std::acos(-1.L);
+
     constexpr long double pi{std::numbers::pi_v<long double>};
     std::cout << "default precision (6): " << pi << '\n'
+
 
               << "std::precision(10):   " << std::setprecision(10) << pi << '\n'
+
    const auto default_precision{std::cout.precision()};
              << "max precision:         "
+
    constexpr auto max_precision{std::numeric_limits<long double>::digits10 + 1};
              << std::setprecision(std::numeric_limits<long double>::digits10)
+
 
              << pi << '\n';
+
     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
 
}
 
}
| output=
+
|output=
default precision (6): 3.14159
+
default precision: 6
std::precision(10):    3.141592654
+
maximum precision: 19
max precision:        3.14159265358979324
+
 
 +
precision: pi:
 +
0  3
 +
1  3
 +
2  3.1
 +
3  3.14
 +
4  3.142
 +
5  3.1416
 +
3.14159
 +
7  3.141593
 +
8  3.1415927
 +
9  3.14159265
 +
10 3.141592654
 +
11  3.1415926536
 +
12  3.14159265359
 +
13  3.14159265359
 +
14  3.1415926535898
 +
15  3.14159265358979
 +
16  3.141592653589793
 +
17  3.1415926535897932
 +
18  3.14159265358979324
 +
19  3.141592653589793239
 
}}
 
}}
 +
 +
===Defect reports===
 +
{{dr list begin}}
 +
{{dr list item|wg=lwg|dr=183|std=C++98|before={{tt|setprecision}} could only be used with streams<br>of type {{lc|std::ostream}} or {{lc|std::istream}}|after=usable with any<br>character stream}}
 +
{{dr list end}}
  
 
===See also===
 
===See also===
 
{{dsc begin}}
 
{{dsc begin}}
{{dsc inc | cpp/io/manip/dsc fixed}}
+
{{dsc inc|cpp/io/manip/dsc fixed}}
 +
{{dsc inc|cpp/io/ios_base/dsc precision}}
 
{{dsc end}}
 
{{dsc end}}
  
[[de:cpp/io/manip/setprecision]]
+
{{langlinks|de|es|fr|it|ja|pt|ru|zh}}
[[es:cpp/io/manip/setprecision]]
+
[[fr:cpp/io/manip/setprecision]]
+
[[it:cpp/io/manip/setprecision]]
+
[[ja:cpp/io/manip/setprecision]]
+
[[pt:cpp/io/manip/setprecision]]
+
[[ru:cpp/io/manip/setprecision]]
+
[[zh:cpp/io/manip/setprecision]]
+

Latest revision as of 23:06, 15 September 2023

 
 
 
Input/output manipulators
Floating-point formatting
setprecision
Integer formatting
Boolean formatting
Field width and fill control
Other formatting
Whitespace processing
Output flushing
(C++20)  

Status flags manipulation
Time and money I/O
(C++11)
(C++11)
(C++11)
(C++11)
Quoted manipulator
(C++14)
 
Defined in header <iomanip>
/*unspecified*/ setprecision( int n );

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.

Contents

[edit] Parameters

n - new value for precision

[edit] Return value

An object of unspecified type such that

  • if out is an object of type std::basic_ostream<CharT, Traits>, the expression out << setprecision(n)
    • has type std::basic_ostream<CharT, Traits>&
    • has value out
    • behaves as if it called f(out, n)
  • if in is an object of type std::basic_istream<CharT, Traits>, the expression in >> setprecision(n)
    • has type std::basic_istream<CharT, Traits>&
    • has value in
    • behaves as if it called f(in, n)

where the function f is defined as:

void f(std::ios_base& str, int n)
{
    // set precision
    str.precision(n);
}

[edit] 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
}

Output:

default precision: 6
maximum precision: 19
 
precision: pi:
 0  3
 1  3
 2  3.1
 3  3.14
 4  3.142
 5  3.1416
 6  3.14159
 7  3.141593
 8  3.1415927
 9  3.14159265
10  3.141592654
11  3.1415926536
12  3.14159265359
13  3.14159265359
14  3.1415926535898
15  3.14159265358979
16  3.141592653589793
17  3.1415926535897932
18  3.14159265358979324
19  3.141592653589793239

[edit] Defect reports

The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

DR Applied to Behavior as published Correct behavior
LWG 183 C++98 setprecision could only be used with streams
of type std::ostream or std::istream
usable with any
character stream

[edit] See also

changes formatting used for floating-point I/O
(function) [edit]
manages decimal precision of floating point operations
(public member function of std::ios_base) [edit]