When we use floating numbers, we sometimes need our C++ software to display them in a consistent way so we can compare numbers next to one another or with the preceding or successive numbers in an order. We can arrange formatting display of numbers both integer and precision side.
How can we format the display of numbers in C++? How can we use fixed, scientific or default formatting in C++ software? How can we use precision when formatting?
The precision of floating numbers (float , double, long double) can be arranged by the precision()
function as given example below,
Table of Contents
How do we format a number’s precision before output in C++?
We can use std::cout.precision() function before using cout as below,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
#include <iostream> #include <cmath> int main() { std::cout.precision(1); std::cout << M_PI <<'\n'; std::cout.precision(2); std::cout << M_PI <<'\n'; std::cout.precision(3); std::cout << M_PI <<'\n'; std::cout.precision(4); std::cout << M_PI <<'\n'; std::cout.precision(5); std::cout << M_PI <<'\n'; std::cout.precision(6); std::cout << M_PI <<'\n'; std::cout.precision(16); std::cout << M_PI <<'\n'; getchar(); } |
here we #include <cmath> library to use pi constant M_PI. Output will be as here,
1 2 3 4 5 6 7 |
3 3.1 3.14 3.142 3.1416 3.14159 3.141592653589793 |
What about number formatting on output?
We can use std::setprecision() function when we use cout as below,
1 2 3 4 5 6 7 8 9 10 11 12 13 |
#include <iostream> #include <iomanip> #include <cmath> int main() { std::cout << "Precision 4: " << std::setprecision(4) << M_PI << '\n' << "Precision 7: " << std::setprecision(7) << M_PI << '\n'; getchar(); } |
1 2 |
Precision 4: 3.1416 Precision 7: 3.14159265 |
Using Specific Number Formatting in our C++ software
We can use std::fixed, std::scientific, std::hexfloat, std::defaultfloat to set format of floating numbers in our outputs. To use these I/O manipulators we need to #include <iomanip> library, We can set the printing format as below;
1 2 3 4 5 |
std::cout << std::fixed; std::cout << std::scientific; std::cout << std::defaultfloat; |
We can also use and change formatting with these I/O manipulators when using cout command. See full example below;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
#include <iostream> #include <iomanip> #include <cmath> int main() { std::cout.precision(6); std::cout << "fixed : " << std::fixed << M_PI << '\n' << "scientific : " << std::scientific << M_PI << '\n' << "defaultfloat: " << std::defaultfloat << M_PI << '\n'; getchar(); } |
and the output will be as here,
1 2 3 |
fixed : 3.141593 scientific : 3.141593e+00 defaultfloat: 3.14159 |
How do we use fixed size formatting in C++?
We can fix the size of display by using std::setw() method. This fixed format with fixed size is the mostly used display in engineering results. See this C++ example below,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
#include <iostream> #include <iomanip> #include <cmath> int main() { double a= 1.123456789; double b= 12.123456789; double c= 123.123456789; std::cout << std::fixed; std::cout << std::setw(10) << std::setprecision(6) << a << '\n'; std::cout << std::setw(10) << std::setprecision(6) << b << '\n'; std::cout << std::setw(10) << std::setprecision(6) << c << '\n'; } |
and the output will be,
1 2 3 |
1.123457 12.123457 123.123457 |
How do we display matrix members with formatting in C++ software?
We can display matrix forms as below,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
void display_7diag_matrix() { for(int i=0; i<NN; i++) { std::cout << std::fixed << std::setw(10) << std::setfill(' ') << std::setprecision(2) <<"|" <<mx[i].A << ", " << mx[i].B << ", " << mx[i].D << ", " << mx[i].E << ", " << mx[i].F << ", " << mx[i].H << ", " << mx[i].I << "| |" << u[i] << "| |" << x[i] << "| |" << sol[i] << "|\n"; } } |
Note that we can use #include <limits>
to get the maximum precision of a float or double.