×

C++ Programs

C++ Most popular & Searched Programs

C++ Basic I/O Programs

C++ Constructor & Destructor Programs

C++ Manipulators Programs

C++ Inheritance Programs

C++ Operator Overloading Programs

C++ File Handling Programs

C++ Bit Manipulation Programs

C++ Classes & Object Programs

C++ program to demonstrate example of setprecision manipulator

Learn about the setprecision manipulator in C++, and demonstrate the use of setprecision manipulator using C++ program.
[Last updated : March 02, 2023]

setprecision Manipulator in C++

The 'setprecision' manipulator is used to control the number of digits of an output stream display of a floating- point value.

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

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.