setprecision & fixed in iomanip of C++

I never printed the floating point numbers correctly in C++, so I hoped there would be library for this. After the search I found iomanip is the rescue. 

Tip:  think we can use this iomanip for all the display operations such as Command Line Interface 

#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
    float a = 3.1789, b = 100.00, c = 103.1;
    
    cout << " Default Precision for floating numbers " << endl; 
    cout << a << " " << b << " " << c << endl;
    
    cout << " Setting the accuracy of the floating numbers " << endl;
    cout << setprecision(2) << a << " " << b << " " << c << endl;
    
    return 0;
}

Output:

1
2
3
4
 Default Precision for floating numbers 
3.1789 100 103.1
 Setting the accuracy of the floating numbers 
3.2 1e+02 1e+02

Merlin Beard!! What in the world the fourth line depicts ?? 

By default compiler took the scientific notation. Since the setprecision wasn't effective still, I planned to use fixed.

#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
    float a = 3.1789, b = 100.00, c = 103.1;
    
    cout << " Default Precision for floating numbers " << endl; 
    cout << a << " " << b << " " << c << endl;
    
    cout << " Setting the accuracy of the floating numbers " << endl;
    cout << setprecision(2) << a << " " << b << " " << c << endl;
    
    cout << " Setting the accuracy of the floating numbers with fixed() " << endl;
    cout << setprecision(2) << fixed << a << " " << b << " " << c << endl;
    
    return 0;
}

Output:

1
2
3
4
5
6
 Default Precision for floating numbers 
3.1789 100 103.1
 Setting the accuracy of the floating numbers 
3.2 1e+02 1e+02
 Setting the accuracy of the floating numbers with fixed() 
3.18 100.00 103.10

This concludes setprecision modifier is effective only if we tell the compiler that if we want to use either fixed precision or scientific notation for the output. 




Now, let's see how to use it on the stringstream. Click here to learn more about stringstream. 

#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
    float a = 3.1789, b = 100.00, c = 103.1;
    stringstream ss;
    
    ss << setprecision(2) << fixed << a << " " <<  b << " " << c << "\n";
    cout << "string stream " << ss.str();
    
    return 0;
}

Output:

1
string stream 3.18 100.00 103.10


Be aware that setprecision and fixed should be set before converting the floating point numbers to string. If not, this would cause a problem like below.

#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
    float a = 3.1789, b = 100.00, c = 103.1;
    stringstream ss;
    
    ss << a << " " <<  b << " " << c << "\n";
    cout << "Before setting precision in string stream " << ss.str();
    
    ss.precision(2);
    cout << "After setting precision in string stream " << ss.str();
    
    return 0;
}

Output:

1
2
Before setting precision in string stream 3.1789 100 103.1
After setting precision in string stream 3.1789 100 103.1

I can resolve this problem by setting the set precision while streaming into a buffer as programmed above. I can also resolve this by another method. 

#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
    float a = 3.1789, b = 100.00, c = 103.1;
    stringstream ss;
    ss.precision(2);
    
    ss << fixed << a << " " <<  b << " " << c << "\n";
    cout << "After setting precision in string stream " << ss.str();
    
    return 0;
}

Output:

1
After setting precision in string stream 3.18 100.00 103.10




Comments