How to Print Numbers With Specified Decimal Points in C++

This article will introduce how to print the numbers with specified decimal points in C++.

Use std::fixed and std::setprecision Methods to Specify Precision

This method uses so-called I/O manipulators from the standard library defined in <iomanip> header (see full list). These functions can modify the stream data and are mostly used for I/O formatting operations. In the first example, we only use the fixed method, which modifies the default formatting so as the floating-point values are written using fixed-point notation. Note, though, the result always has a predefined precision of 6 points.

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

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

int main() {
    vector<double> d_vec = {123.231, 2.2343, 0.324, 10.222424,
                            6.3491092019, 110.12329403024,
                            92.001112, 0.000000124};

    for (auto &d : d_vec) {
        cout << fixed << d << endl;
    }
    return EXIT_SUCCESS;
}

Output:

123.231000
2.234300
0.324000
10.222424
6.349109
110.123294
92.001112
0.000000

Alternatively, we can fixate the decimal points after the comma and specify the precision value simultaneously. In the next code example, the fixed function is called together with setprecision, which itself takes int value as a parameter.

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

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

int main() {
    vector<double> d_vec = {123.231, 2.2343, 0.324, 10.222424,
                            6.3491092019, 110.12329403024,
                            92.001112, 0.000000124};

    for (auto &d : d_vec) {
        cout << fixed
            << setprecision(3) << d << endl;
    }
    return EXIT_SUCCESS;
}

Output:

123.231
2.234
0.324
10.222
6.349
110.123
92.001
0.000

The previous code sample solves the initial problem, but the output seems quite mixed, and it’s hardly in readable formatting. We can use std::setw and std:setfill functions to display float values in a given vector aligned to the same comma position. To achieve this, we should call the setw function to tell the cout the maximum length of each output (we choose 8 in this case). Next, the setfill function is called to specify a char that will fill the padding places. Finally, we add previously used methods (fixed and setprecision) and output to console.

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

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

int main() {
    vector<double> d_vec = {123.231, 2.2343, 0.324, 10.222424,
                            6.3491092019, 110.12329403024,
                            92.001112, 0.000000124};

    for (auto &d : d_vec) {
        cout << setw(8) << setfill(' ') << fixed
            << setprecision(3) << d << endl;
    }
    return EXIT_SUCCESS;
}

Output:

123.231
  2.234
  0.324
 10.222
  6.349
110.123
 92.001
  0.000

Related Article - C++ Float

  • How to Convert Float to Int in C++