Để in số thực với số chữ số thập phân xác định, C++ cung cấp setprecision kết hợp với cout (thư viện <iomanip>).
Ví dụ minh họa
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
double x = 2.3654789;
cout << "3 chữ số: " << setprecision(3) << x << endl;
cout << "2 chữ số: " << setprecision(2) << x << endl;
cout << "7 chữ số: " << setprecision(7) << x << endl;
}Kết quả
3 chữ số: 2.365 2 chữ số: 2.37 7 chữ số: 2.3654789