How to Use setprecision in C++

Jinku Hu Feb 02, 2024
  1. Use setprecision() Method to Set Custom Precision for Floats
  2. Use setprecision() and std::fixed() to Set Custom Precision for Floats
  3. Use setprecision() and std::fixed() to Align Floats to a Decimal Point
How to Use setprecision in C++

This article will demonstrate multiple methods of how to use the setprecision method in C++.

Use setprecision() Method to Set Custom Precision for Floats

setprecision() is part of I/O manipulators library <iomanip> and can be used to modify default precision of the floating-point numbers. setprecision() is usually used in expressions with I/O streams.

The following example shows how to set float number precision for the cout output stream object. Notice that, setprecision() applies to the whole number (integer part and fractional part) and uses scientific notation when numbers have a bigger magnitude than the specified precision.

#include <iomanip>
#include <iostream>
#include <vector>

using std::cout;
using std::endl;
using std::fixed;
using std::setprecision;
using std::vector;

int main() {
  vector<double> d_vec = {123.231,       2.2343, 0.324,     0.012,
                          26.9491092019, 11013,  92.001112, 0.000000234};

  for (auto &i : d_vec) {
    cout << setprecision(3) << i << " | ";
  }
  cout << endl;

  return EXIT_SUCCESS;
}

Output:

123 | 2.23 | 0.324 | 0.012 | 26.9 | 1.1e+04 | 92 | 2.34e-07 |

Use setprecision() and std::fixed() to Set Custom Precision for Floats

Alternatively, we can use setprecision() and fixed() stream manipulators in conjunction to print floating-point values with the same number of digits after the decimal point. The fixed() method sets the fractional part of the number to a fixed length, which by default is 6 digits. In the following code sample, we output to cout stream and call both manipulators before the number is inserted into the output.

#include <iomanip>
#include <iostream>
#include <vector>

using std::cout;
using std::endl;
using std::fixed;
using std::setprecision;
using std::vector;

int main() {
  vector<double> d_vec = {123.231,       2.2343, 0.324,     0.012,
                          26.9491092019, 11013,  92.001112, 0.000000234};

  for (auto &i : d_vec) {
    cout << fixed << setprecision(3) << i << " | ";
  }
  cout << endl;

  return EXIT_SUCCESS;
}

Output:

123.231 | 2.234 | 0.324 | 0.012 | 26.949 | 11013.000 | 92.001 | 0.000 |

Use setprecision() and std::fixed() to Align Floats to a Decimal Point

Finally, we can combine the setw, right, setfill, fixed and setprecision manipulators to output floating-point numbers aligned to the decimal point. setw method specifies the output stream width with the number of characters passed as an argument. setfill sets a character by which the unused space will be filled and right method tells cout the side to which filling operation applies.

#include <iomanip>
#include <iostream>
#include <vector>

using std::cout;
using std::endl;
using std::fixed;
using std::setprecision;
using std::vector;

int main() {
  vector<double> d_vec = {123.231,       2.2343, 0.324,     0.012,
                          26.9491092019, 11013,  92.001112, 0.000000234};

  for (auto &i : d_vec) {
    cout << std::setw(10) << std::right << std::setfill(' ') << fixed
         << setprecision(4) << i << endl;
  }
  cout << endl;

  return EXIT_SUCCESS;
}

Output:

  123.2310
    2.2343
    0.3240
    0.0120
   26.9491
11013.0000
   92.0011
    0.0000
Author: Jinku Hu
Jinku Hu avatar Jinku Hu avatar

Founder of DelftStack.com. Jinku has worked in the robotics and automotive industries for over 8 years. He sharpened his coding skills when he needed to do the automatic testing, data collection from remote servers and report creation from the endurance test. He is from an electrical/electronics engineering background but has expanded his interest to embedded electronics, embedded programming and front-/back-end programming.

LinkedIn Facebook

Related Article - C++ Float