tags:

views:

156

answers:

2

I want cout to output an int with leading zeros, so the value 1 would be printed as 001 and the value 25 printed as 025 .. you get the idea .. how can I do this?

Thanks.

+10  A: 
cout << setfill('0') << setw(5) << 25;

output:
00025

setfill is set to space ' ' by default. setw sets the width of the field to be printed, and that's it.

don't forget to include <iomanip> :)


If you are interested in knowing how the to format output streams in general, I wrote an answer for another question, hope it is useful: Formatting C++ Console Output.

AraK
+1  A: 
cout.fill( '0' );    
cout.width( 3 );
cout << value;
Evän Vrooksövich