Precision-function-in-C++ .

In this session i have discuss precision function in C++ .


The precision() function is used to define how many digit you want to display from decimal point . It is a member function .

Precision() syntax

    cout.precision(d);
    

Where d is number of digit .


Example : Write a program that demonstrate the precision function .

    #include<iostream>
    using namespace std;
    int main()
    {  
          double x=2.11111;
          cout<<"Without spcified precision\n";
          cout<<x<<"\n";
          cout<<"After spcified precision\n";
          cout.precision(3);
          return 0;
    }
    
    OUTPUT : Without specified precision
             2.11111
             After specified precision
             2.11
           

The above program we have specify 3 digits but it will display only two digits after decimal point . It is always display less than one to specified digit .


Welcome to AndroidATPoint.

back image                     next image