Q:

C++ program to demonstrate example of setprecision manipulator

belongs to collection: C++ Manipulators programs

0

C++ program to demonstrate example of setprecision manipulator

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

setprecision manipulator program in C++

 

/*C++ program to demonstrate example of setprecision manipulator.*/
 
#include <iostream>
#include <iomanip>
using namespace std;
 
int main()
{
    float val=123.456f;
     
    cout << "Value of val with different setprecision parameters:" << endl;
    cout << setprecision( 3) << val << endl;
    cout << setprecision( 4) << val << endl;
    cout << setprecision( 5) << val << endl;
    cout << setprecision( 6) << val << endl;
    cout << setprecision( 7) << val << endl;
    cout << setprecision( 8) << val << endl;
     
    cout << "Left padded values:" << endl;
    cout << setw(7) << setfill('0') << setprecision( 3) << val << endl;
    cout << setw(7) << setfill('0') << setprecision( 4) << val << endl;
    cout << setw(7) << setfill('0') << setprecision( 5) << val << endl;
    cout << setw(7) << setfill('0') << setprecision( 6) << val << endl;
    cout << setw(7) << setfill('0') << setprecision( 7) << val << endl;
    cout << setw(7) << setfill('0') << setprecision( 8) << val << endl;
     
    return 0;
}

Output

    Value of val with different setprecision parameters:
    123
    123.5
    123.46
    123.456
    123.456
    123.456
    Left padded values:
    0000123
    00123.5
    0123.46
    123.456
    123.456
    123.456

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C++ program to demonstrate example of setbase mani... >>
<< C++ program to demonstrate example of setfill mani...