Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

formatting an integer output using ostream

Tags:

c++

I am trying to find the equivalence of %2d using cout << <<endl format. how can I do that?

like image 253
aherlambang Avatar asked Apr 16 '10 02:04

aherlambang


2 Answers

The header <iomanip> contains the stream manipuator setw:

cout << setw(2) << myint << endl

You may also be interested in setfill:

cout << setfill('0') << setw(2) << myint << endl

will pad with a 0 instead of a space.

like image 184
John Ledbetter Avatar answered Sep 22 '22 21:09

John Ledbetter


You need setw

like image 36
codaddict Avatar answered Sep 23 '22 21:09

codaddict