C++ setprecision() Manipulator

Beginner
5 min read
Updated: Nov 2026
3 examples
iomanip

Definition

In C++, setprecision(n) sets how many digits a stream prints for floating-point numbers. What n counts depends on the format: all significant digits by default, or digits after the point when fixed or scientific is on.

The setting stays on the stream until you change it. The default value is 6. Include <iomanip> to use it.

This is a manipulator with an argument. You write it inside a stream chain, like std::cout << setprecision(...), and it changes how the stream treats the data. It lives in <iomanip>, so remember to include that header.

Syntax

Put std::setprecision(n) into the stream. Add std::fixed for digits after the decimal point.

C++
#include <iomanip>

std::cout << std::setprecision(3) << 3.14159;                 // 3.14
std::cout << std::fixed << std::setprecision(3) << 3.14159;   // 3.142
CallWhat it does
std::cout << std::setprecision(3) << 3.14159Three significant digits: 3.14.
std::cout << std::fixed << std::setprecision(3) << 3.14159Three digits after the point: 3.142.
std::cout << std::setprecision(10)Shows up to ten digits.

Printing rounds the number only for the screen. The variable keeps its value.

Library declarations
C++
/* unspecified */ setprecision(int n);

Parameters

ParameterDescription
nRequired. The number of digits. Without fixed or scientific it counts all significant digits. With them it counts digits after the point.

Return Value

Return ValueDescription
Unspecified typeA small helper object that sets the precision when it is sent into a stream. You never name its type. Just write setprecision(n) inside the chain.

C++ Examples

Example 1 — setprecision Without fixed

The number counts the digits in total, so large numbers switch to scientific form.

C++
#include <iomanip>
#include <iostream>

int main() {
    std::cout << std::setprecision(3);

    std::cout << 3.14159 << ' ' << 1234.5678 << '\n';
    return 0;
}

How It Works

  1. setprecision(3) allows three significant digits for each number.
  2. 3.14159 keeps three digits and prints as 3.14.
  3. 1234.5678 has four digits before the point, so it cannot fit. It is shown in scientific form as 1.23e+03.

Example 2 — setprecision with fixed

With fixed, the number counts digits after the point.

C++
#include <iomanip>
#include <iostream>

int main() {
    std::cout << std::fixed << std::setprecision(3);

    std::cout << 3.14159 << ' ' << 1234.5678 << '\n';
    return 0;
}

How It Works

  1. fixed turns on plain decimal form and setprecision(3) sets three digits after the point.
  2. 3.14159 is rounded to 3.142.
  3. 1234.5678 keeps all its whole digits and is rounded to 1234.568. No scientific form appears.

Example 3 — Print Pi with 1 to 4 Digits

A loop shows how each value changes the output.

C++
#include <iomanip>
#include <iostream>

int main() {
    double pi = 3.14159;

    for (int p = 1; p <= 4; p++) {
        std::cout << "precision " << p << ": "
                  << std::setprecision(p) << pi << '\n';
    }
    return 0;
}

How It Works

  1. In each round of the loop, setprecision(p) sets a new number of significant digits.
  2. With one digit, the number is 3. With two, 3.1. Every extra digit adds one more place.
  3. The last digit is rounded, not cut. With four digits, 3.14159 becomes 3.142.

Frequently Asked Questions

It sets the number of digits printed for floating-point numbers. The default is 6.
Use cout << fixed << setprecision(2) << value. Without fixed, the 2 would count all digits.
Without fixed, the precision counts significant digits. If the number does not fit, the stream switches to scientific form. Add std::fixed to avoid it.
No. It only changes how the number is printed.
Yes. Add #include <iomanip>. You can also call cout.precision(n) without it.

Next: setw()

Set the field width for the next item only.

setw() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

13 people found this page helpful