Welcome to collectivesolver - Programming & Software Q&A. A website you can trust. All programs tested and works. Contact: aviboots(AT)netvision.net.il
Hostgator 1 Penny

BlueHost Web Hosting

Ecommerce Software - Best Ecommerce Platform Made for You - Free Trial

Ecommerce Software - Best Ecommerce Platform Made for You - Free Trial

getflywheel wordpress hosting


Distribute your Resume to 500+ Job Boards & Recruiters


7,621 questions

10,674 answers

573 users

How to provides precision to floating point numbers in C++

1 Answer

0 votes
#include <iostream>
#include <iomanip>

using namespace std;

int main()
{
	double pi = 3.1415926, negative_pi = -3.1415926;

	cout << fixed << setprecision(0) << pi << "  " << negative_pi << endl;
	cout << fixed << setprecision(1) << pi << "  " << negative_pi << endl;
	cout << fixed << setprecision(2) << pi << "  " << negative_pi << endl;
	cout << fixed << setprecision(3) << pi << "  " << negative_pi << endl;
	cout << fixed << setprecision(4) << pi << "  " << negative_pi << endl;
	cout << fixed << setprecision(5) << pi << "  " << negative_pi << endl;
	cout << fixed << setprecision(6) << pi << "  " << negative_pi << endl;
	cout << fixed << setprecision(7) << pi << "  " << negative_pi << endl;

	return 0;
}


/*
run:

3  -3
3.1  -3.1
3.14  -3.14
3.142  -3.142
3.1416  -3.1416
3.14159  -3.14159
3.141593  -3.141593
3.1415926  -3.1415926

*/

 




answered May 18 by avibootz
...