How can I pad an int with leading zeros when using cout << operator?

278,301

Solution 1

With the following,

#include <iomanip>
#include <iostream>

int main()
{
    std::cout << std::setfill('0') << std::setw(5) << 25;
}

the output will be

00025

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


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.

Solution 2

Another way to achieve this is using old printf() function of C language

You can use this like

int dd = 1, mm = 9, yy = 1;
printf("%02d - %02d - %04d", mm, dd, yy);

This will print 09 - 01 - 0001 on the console.

You can also use another function sprintf() to write formatted output to a string like below:

int dd = 1, mm = 9, yy = 1;
char s[25];
sprintf(s, "%02d - %02d - %04d", mm, dd, yy);
cout << s;

Don't forget to include stdio.h header file in your program for both of these functions

Thing to be noted:

You can fill blank space either by 0 or by another char (not number).
If you do write something like %24d format specifier than this will not fill 2 in blank spaces. This will set pad to 24 and will fill blank spaces.

Solution 3

cout.fill('*');
cout << -12345 << endl; // print default value with no field width
cout << setw(10) << -12345 << endl; // print default with field width
cout << setw(10) << left << -12345 << endl; // print left justified
cout << setw(10) << right << -12345 << endl; // print right justified
cout << setw(10) << internal << -12345 << endl; // print internally justified

This produces the output:

-12345
****-12345
-12345****
****-12345
-****12345

Solution 4

In C++20 you'll be able to do:

std::cout << std::format("{:03}", 25); // prints 025

In the meantime you can use the {fmt} library, std::format is based on.

Disclaimer: I'm the author of {fmt} and C++20 std::format.

Solution 5

cout.fill( '0' );    
cout.width( 3 );
cout << value;
Share:
278,301

Related videos on Youtube

jamieQ
Author by

jamieQ

Updated on December 16, 2020

Comments

  • jamieQ
    jamieQ over 3 years

    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. How can I do this?

  • shashwat
    shashwat over 11 years
    but.. how can I write formatted output to a string (char* or char[]) not to console directly. Actually I am writing a function that returns formatted string
  • Khaled Alshaya
    Khaled Alshaya over 11 years
    @Shashwat Tripathi Use std::stringstream.
  • shashwat
    shashwat over 11 years
    @AraK I think this would not work in Turbo C++. I used it using sprintf(s, "%02d-%02d-%04d", dd, mm, yy); where s is char* and dd, mm, yy are of int type. This will write 02-02-1999 format according to the values in variables.
  • Magnus
    Magnus over 10 years
    I know this is an old answer, but it should still be pointed out that sprintf should generally not be trusted too much since you can't specify the length of the buffer it's supposed to write to. Using snprintf tends to be safer. Using streams as opposed to *printf() is also much more type safe because the compiler has a chance to check the parameters' types at compile time; AraK's accepted answer is both type safe and "standard" C++, and it doesn't rely on headers that poison the global namespace.
  • vitaut
    vitaut over 3 years
    @jlh, this is a library, not compiler feature but otherwise you are right: std::format is not supported by standard library implementations yet (C++20 has only recently been published). I know that libc++ and Microsoft work on it.
  • Alex Ozer
    Alex Ozer about 3 years
    Congrats on getting your library into C++20! First used it many years ago.