ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

Home » C+ solved programs » C++ Manipulators programs

C++ program to demonstrate example of setprecision manipulator

This program will demonstrate example of setprecision manipulator in c++ programming language.

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
ADVERTISEMENT
ADVERTISEMENT




Comments and Discussions

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.