Friday 17 July 2015

setprecision Manipulator :


The 'setprecision' manipulator is used to set the number of digits to be displayed after decimal point. It is applied to all subsequent floating point numbers written to that output stream. The value is rounded with the use of this manipulator.

Syntax :


The syntax of 'setprecision' manipulator is as follows :

      setprecision (n)

The n indicates the number of digits displayed after the decimal point. It can be an unsigned positive integer constant, variable and expression.

Example : 

                       #include <iostream.h>
                    #include <conio.h>
                    #include <iomanip.h>
void main(){

                   double r, n1 = 132.364, n2 = 26.91;
                r = n1 / n2;

     cout<<r<<endl;
     cout<<setprecision(5)<<r<<endl;
     cout<<setprecision(4)<<r<<endl;
     cout<<setprecision(3)<<r<<endl;
     cout<<setprecision(2)<<r<<endl;
     cout<<setprecision(1)<<r<<endl;

getch();
}

Output :

                      4.918766
                      4.91877
                      4.9188
                      4.919
                      4.92
                      4.9

No comments:

Post a Comment