# std::ios_base::precision

> C++ Atlas: `cppatlas.com/w/cpp/io/ios_base/precision` | Source: https://en.cppreference.com/w/cpp/io/ios_base/precision | Domain: Input/output

Manages the precision (i.e. how many digits are generated) of floating point output performed by std::num_put::do_put.

## Official Specification Summary (cppreference)

```cpp
streamsize precision() const; // (1)
streamsize precision( streamsize new_precision ); // (2)
```

Manages the precision (i.e. how many digits are generated) of floating point output performed by `std::num_put::do_put`.

1) Returns the current precision.

2) Sets the precision to the given one. Returns the previous precision.

The default precision, as established by `std::basic_ios::init`, is 6.

### Parameters

- `new_precision` - new precision setting

### Return value

The precision before the call to the function

### Example

```cpp
#include <iostream>

int main()
{
    const double d = 12.345678901234;
    std::cout << "The  default precision is " << std::cout.precision() << "\n\n";
    std::cout << "With default precision d is " << d << '\n';
    std::cout.precision(8);
    std::cout << "With high    precision d is " << d << '\n';
}
```

Output:

```text
The  default precision is 6

With default precision d is 12.3457
With high    precision d is 12.345679
```

### 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 189 | C++98 | 'precision' was defined as 'the number of digits after the decimal point', but it is not correct in some cases | corrected |

### See also

- [`width`](https://en.cppreference.com/w/cpp/io/ios_base/width): manages field width (public member function)
- [`setprecision`](https://en.cppreference.com/w/cpp/io/manip/setprecision): changes floating-point precision (function)

---
Attribution: content from https://en.cppreference.com/w/cpp/io/ios_base/precision (cppreference.com), licensed under CC-BY-SA 3.0 and GFDL. Modified: converted to markdown, study notes added. Offline snapshot of 9 February 2025.