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?
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.
-
4but.. 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 – shashwat Dec 23 '12 at 9:32 -
15
-
10don't forget to restore the stream format after doing that or you'll get a nasty surprise later. – Code Abominator Apr 15 '15 at 0:03
-
16This answer pointed me in the right direction but it could be improved. To actually use this code, you will need to include
<iostream>
and<iomanip>
at the top of your file, and you will need to writeusing namespace std;
, but that's bad practice so maybe instead you should prefix the three identifiers in this answer withstd::
. – David Grayson Jul 6 '15 at 17:44 -
@shashwat you can use following code - std::stringstream filename; filename.fill('0'); filename.width(5); filename<<std::to_string(i); – Prince Patel Feb 25 '19 at 16:14
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.
-
12I 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. – Magnus Feb 5 '14 at 14:19
-
The answer is using date formatting as an example. Note, however, that it's using an exotic time format as an example, even though it looks similar to ISO_8601 on the surface (en.wikipedia.org/wiki/ISO_8601). – varepsilon Apr 15 '19 at 12:04
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
cout.fill( '0' );
cout.width( 3 );
cout << value;
-
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 – shashwat Dec 23 '12 at 9:33 -
2
-
@AraK I think this would not work in Turbo C++. I used it using
sprintf(s, "%02d-%02d-%04d", dd, mm, yy);
wheres
ischar*
anddd, mm, yy
are ofint
type. This will write02-02-1999
format according to the values in variables. – shashwat Dec 23 '12 at 12:44
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
.
-
If I understand correctly, not a single compiler currently supports this? Source: en.cppreference.com/w/cpp/20 – jlh Dec 31 '20 at 16:46
-
1@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. – vitaut Dec 31 '20 at 19:32
Another example to output date and time using zero as a fill character on instances of single digit values: 2017-06-04 18:13:02
#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <ctime>
using namespace std;
int main()
{
time_t t = time(0); // Get time now
struct tm * now = localtime(&t);
cout.fill('0');
cout << (now->tm_year + 1900) << '-'
<< setw(2) << (now->tm_mon + 1) << '-'
<< setw(2) << now->tm_mday << ' '
<< setw(2) << now->tm_hour << ':'
<< setw(2) << now->tm_min << ':'
<< setw(2) << now->tm_sec
<< endl;
return 0;
}
I would use the following function. I don't like sprintf
; it doesn't do what I want!!
#define hexchar(x) ((((x)&0x0F)>9)?((x)+'A'-10):((x)+'0'))
typedef signed long long Int64;
// Special printf for numbers only
// See formatting information below.
//
// Print the number "n" in the given "base"
// using exactly "numDigits".
// Print +/- if signed flag "isSigned" is TRUE.
// Use the character specified in "padchar" to pad extra characters.
//
// Examples:
// sprintfNum(pszBuffer, 6, 10, 6, TRUE, ' ', 1234); --> " +1234"
// sprintfNum(pszBuffer, 6, 10, 6, FALSE, '0', 1234); --> "001234"
// sprintfNum(pszBuffer, 6, 16, 6, FALSE, '.', 0x5AA5); --> "..5AA5"
void sprintfNum(char *pszBuffer, int size, char base, char numDigits, char isSigned, char padchar, Int64 n)
{
char *ptr = pszBuffer;
if (!pszBuffer)
{
return;
}
char *p, buf[32];
unsigned long long x;
unsigned char count;
// Prepare negative number
if (isSigned && (n < 0))
{
x = -n;
}
else
{
x = n;
}
// Set up small string buffer
count = (numDigits-1) - (isSigned?1:0);
p = buf + sizeof (buf);
*--p = '\0';
// Force calculation of first digit
// (to prevent zero from not printing at all!!!)
*--p = (char)hexchar(x%base);
x = x / base;
// Calculate remaining digits
while(count--)
{
if(x != 0)
{
// Calculate next digit
*--p = (char)hexchar(x%base);
x /= base;
}
else
{
// No more digits left, pad out to desired length
*--p = padchar;
}
}
// Apply signed notation if requested
if (isSigned)
{
if (n < 0)
{
*--p = '-';
}
else if (n > 0)
{
*--p = '+';
}
else
{
*--p = ' ';
}
}
// Print the string right-justified
count = numDigits;
while (count--)
{
*ptr++ = *p++;
}
return;
}