How to print a double value with full precision using cout in C++

3 Answers

0 votes
#include <iostream>
 
int main() {
    double f = 3.141592653589793;
    
    std::cout.unsetf(std::ios::floatfield);             
    std::cout.precision(16);
    std::cout << f << "\n";
 
    std::cout.precision(14);
    std::cout << f << "\n";
   
    std::cout.setf(std::ios::fixed, std:: ios::floatfield); 
    std::cout << f << '\n';
}

  
  
/*
run:
  
3.141592653589793
3.1415926535898
3.14159265358979
 
*/
    

 



answered Jan 8, 2022 by avibootz
edited Jun 5, 2024 by avibootz
0 votes
#include <iostream>
#include <iomanip>

int main() {
    std::cout << std::setprecision(16) << 3.141592653589793 << std::endl;
}

    
          
/*
run:
       
3.141592653589793

*/

 



answered Jun 5, 2024 by avibootz
0 votes
#include <iostream>
#include <numbers>
#include <format>
 
int main()
{
    std::cout << std::format("{}", std::numbers::pi_v<double>); // C++ 20
}
 
 
/*
run:

3.141592653589793 

*/

 



answered Jun 5, 2024 by avibootz

Related questions

1 answer 171 views
2 answers 213 views
1 answer 91 views
1 answer 144 views
2 answers 115 views
115 views asked Feb 21, 2022 by avibootz
1 answer 212 views
1 answer 167 views
...