Ashish Deb Ashish Deb - 2 months ago 11
C++ Question

How can I print a specific number of digits after decimal point in cpp?

How can I print a specific number of digits after decimal point in cpp?

As like, if I want to print more than 30 digits after decimal point while dividing 22 by 7 then how can I? Plz!

Answer

Below is working code snippet of printing a certain amount of decimal points. So couple things to note: 1)Library needed is iomanip. 2) fixed means everything after decimal point 3) setprecision() means number of digits.

If you don't put fixed then it would count whole numbers before the decimal point as well. However since you want 30 AFTER decimal point you put fixed and setprecision(30).

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

int main()
{
   double answer = 22.0/7.0;
   cout << "22.0 / 7.0 = " << fixed << setprecision(30) << answer << endl;

   return 0;
}
Comments