What is the equivalent of %.2f (printf("%.2f" ,f)) in C++

2 Answers

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

using namespace std;

int main()
{
	float pi = 3.14159265359;
	
	cout << fixed << setprecision(2) << pi << endl;

	return 0;
}


/*
run:

3.14

*/

 



answered Dec 2, 2016 by avibootz
0 votes
#include <iostream>
#include <iomanip>

using namespace std;

int main()
{
	cout << fixed << setprecision(2) << 1/2.0 << endl;

	return 0;
}


/*
run:

0.50

*/

 



answered Dec 2, 2016 by avibootz

Related questions

2 answers 120 views
1 answer 114 views
114 views asked May 14, 2022 by avibootz
1 answer 142 views
1 answer 203 views
203 views asked Dec 30, 2021 by avibootz
1 answer 567 views
2 answers 285 views
...