ERJAN
ERJAN

Reputation: 24500

How to print floating with 2 decimals using cout in C++

I only see how to do it using printf:

printf(" salary = %.2f" , salary) ;

But how to specify it in standard cout function? Do any coders know?

Upvotes: 0

Views: 184

Answers (3)

ERJAN
ERJAN

Reputation: 24500

cout<< fixed <<setprecision(2)<< result<<endl ;

This is the right answer - fixed must be there!

Upvotes: 0

Conor Linehan
Conor Linehan

Reputation: 448

Use cout.precision()

eg.

 int main ()
 {
 cout.precision(5);
 double i = 1.23556456123;
 cout << i;
 }

Upvotes: 1

Joe Z
Joe Z

Reputation: 17936

std::cout << std::setprecision(2) << salary;

More here: http://www.cplusplus.com/reference/iomanip/setprecision/

You might want to check out the rest of the iomanip stuff too: http://www.cplusplus.com/reference/iomanip/

Upvotes: 2

Related Questions