c++ precision

C++ precision example:

#include<iostream>
#include<iomanip>

using namespace std;

int main()
{
    double f;
    f=3.12159;

    cout<<f<<'\n';
    cout<<setprecision(3)<<f<<endl;
    cout<<setprecision(5)<<f<<endl;

    return 0;


}

In this link everything explained clearly….You can check

http://ltcpp.blogspot.com/2012/07/input-output-io-in-c.html

And basically if we want to make just like C in C++ fixed value after decimal point then it can be like this

In C:

#include<stdio.h>
int main()
{
float f;
f=3.5464;

printf("%.2f",f);


    return 0;
}

In C++:

#include<iostream>
#include<iomanip>
using namespace std;

int main()
{
    float f;
f=3.5464;

cout.setf(ios::fixed);
cout<<setprecision(2)<<f;

    return 0;
}

Both C and C++ will give the same result output:3.55

It would be a great help, if you support by sharing :)
Author: zakilive

Leave a Reply

Your email address will not be published.