উত্তর:
নিম্নলিখিত সহ,
#include <iomanip>
#include <iostream>
int main()
{
std::cout << std::setfill('0') << std::setw(5) << 25;
}
আউটপুট হবে
00025
setfill' 'ডিফল্টরূপে স্থান অক্ষর ( ) এ সেট করা হয় । setwক্ষেত্রটির প্রস্থ মুদ্রণের জন্য সেট করে এবং এটি এটি।
যদি আপনি কীভাবে সাধারণভাবে আউটপুট স্ট্রিমগুলি ফর্ম্যাট করতে হয় তা জানতে আগ্রহী হন, আমি অন্য প্রশ্নের জন্য একটি উত্তর লিখেছিলাম, আশা করি এটি কার্যকর: সি ++ কনসোল আউটপুট ফর্ম্যাট করা ।
char* or char[]) সরাসরি কনসোল না করে। প্রকৃতপক্ষে আমি একটি ফাংশন লিখছি যা ফর্ম্যাট স্ট্রিংটি ফিরিয়ে দেয়
<iostream>এবং <iomanip>আপনার ফাইল উপরের, এবং আপনি লেখার প্রয়োজন হবে using namespace std;, কিন্তু যে পরিবর্তে আপনার সাথে এই উত্তরে তিন শনাক্তকারী পূর্বে ভী উচিত খারাপ অভ্যাস তাই হয়তো std::।
এটি অর্জনের আরেকটি উপায় হ'ল printf()সি ভাষার পুরানো ফাংশন ব্যবহার করা
আপনি এই মত ব্যবহার করতে পারেন
int dd = 1, mm = 9, yy = 1;
printf("%02d - %02d - %04d", mm, dd, yy);
এটি 09 - 01 - 0001কনসোলে মুদ্রণ করবে ।
আপনি sprintf()নীচের মত স্ট্রিং এ ফর্ম্যাট আউটপুট লিখতে অন্য ফাংশন ব্যবহার করতে পারেন :
int dd = 1, mm = 9, yy = 1;
char s[25];
sprintf(s, "%02d - %02d - %04d", mm, dd, yy);
cout << s;
stdio.hএই দুটি ফাংশনের জন্য আপনার প্রোগ্রামে শিরোলেখ ফাইলটি অন্তর্ভুক্ত করতে ভুলবেন না
আপনি শূন্য স্থানটি 0 দ্বারা বা অন্য চরের মাধ্যমে (সংখ্যা নয়) পূরণ করতে পারেন।
আপনি যদি %24dফর্ম্যাট স্পেসিফায়ারের মতো কিছু লিখে থাকেন তবে এটি শূন্যস্থান পূরণ করবে না 2। এটি প্যাড সেট করবে 24এবং ফাঁকা স্থান পূরণ করবে।
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
এটি আউটপুট উত্পাদন করে:
-12345
****-12345
-12345****
****-12345
-****12345
cout.fill( '0' );
cout.width( 3 );
cout << value;
char* or char[]) সরাসরি কনসোল না করে। প্রকৃতপক্ষে আমি একটি ফাংশন লিখছি যা ফর্ম্যাট স্ট্রিংটি ফিরিয়ে দেয়
std::stringstream।
sprintf(s, "%02d-%02d-%04d", dd, mm, yy);যেখানে sহয় char*এবং dd, mm, yyহয় intপ্রকার। এটি 02-02-1999ভেরিয়েবলের মান অনুযায়ী ফর্ম্যাট লিখবে ।
আমি নিম্নলিখিত ফাংশন ব্যবহার করব। আমি পছন্দ করি না sprintf; আমি যা চাই তা তা করে না !!
#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;
}
একক অঙ্কের মানগুলির উদাহরণগুলিতে পূরণের অক্ষর হিসাবে শূন্য ব্যবহার করে আউটপুট তারিখ এবং সময় দেওয়ার আরেকটি উদাহরণ: 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;
}