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

1 answer 160 views
1 answer 230 views
230 views asked Dec 30, 2021 by avibootz
2 answers 143 views
1 answer 123 views
123 views asked May 14, 2022 by avibootz
1 answer 587 views
...