C++ की iomanip Library
In this page:
#include <iomanip>
std::cout << std::setw(width) << value;
std::cout << std::setfill('fill_char') << std::setw(width) << value;
setw से Width Set करना
setw(n) print होने वाली अगली value के लिए n characters का एक fixed-width field reserve करता है, इसे spaces से pad करते हुए ताकि output के columns neatly line up हों।
यह सिर्फ अगले single output operation को affect करता है, इसलिए अगर आपको aligned columns वाली पूरी table चाहिए तो हर value से पहले इसे दोबारा call करना होगा।
उदाहरण: Setting Width with setw
// Include std::cout and std::cin
#include <iostream>
#include <iomanip>
// Program execution starts in main()
int main() {
// Print to the console with cout
std::cout << std::setw(10) << "Name" << std::setw(10) << "Score" << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
setfill से Fill Characters Set करना
setfill(c) बदलता है कि कौन सा character setw की छोड़ी empty space भरता है — default से spaces, लेकिन आप numbers को zero-pad करने के लिए 0 या एक visual separator के लिए '*' इस्तेमाल कर सकते हैं।
यह setw के उलट, जो सिर्फ एक बार apply होता है, आपके इसे दोबारा बदलने तक बाद की सारी setw calls के लिए effect में रहता है।
उदाहरण: Setting Fill Characters with setfill
// Include std::cout and std::cin
#include <iostream>
#include <iomanip>
// Program execution starts in main()
int main() {
// Print to the console with cout
std::cout << std::setfill('0') << std::setw(5) << 42 << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Floating Point Precision
setprecision(n) cap करता है कि floating-point output के लिए कितने significant digits (या decimal places, fixed के साथ combined होने पर) दिखते हैं, जो 3.14159265358979 जैसे digits की लंबी, गंदी strings रोकता है।
यह जब भी आप एक user को averages या percentages जैसी computed values display कर रहे हों essential है।
उदाहरण: Floating Point Precision
// Include std::cout and std::cin
#include <iostream>
#include <iomanip>
// Program execution starts in main()
int main() {
// Declare average and set it to 3.14159265358979
double average = 3.14159265358979;
// Print to the console with cout
std::cout << std::setprecision(4) << average << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Left और Right Alignment
left और right control करते हैं कि text एक setw-defined field के किस side से चिपके — left text को शुरुआत में push करता है padding बाद में trail करते हुए, जबकि right (default) text से पहले pad करता है ताकि यह field के right edge के against align हो।
इनके बीच switch करना ही आपको same table में left-aligned labels और right-aligned numbers दोनों बनाने देता है।
उदाहरण: Left and Right Alignment
// Include std::cout and std::cin
#include <iostream>
#include <iomanip>
// Program execution starts in main()
int main() {
// Print to the console with cout
std::cout << std::left << std::setw(10) << "Name" << std::endl;
// Print to the console with cout
std::cout << std::right << std::setw(10) << "Score" << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Fixed Point Notation
fixed को setprecision(2) के साथ combine करना floating-point output को actual value की precision की परवाह किए बिना exactly दो decimal places पर lock कर देता है, जो $19.99 जैसी currency display करने के लिए exactly वह format है जिसकी उम्मीद है।
fixed के बिना, अकेला setprecision specifically decimal places की बजाय total significant digits control करता है, जो 100.5 जैसी values के लिए unexpected results produce कर सकता है।
उदाहरण: Fixed Point Notation
// Include std::cout and std::cin
#include <iostream>
#include <iomanip>
// Program execution starts in main()
int main() {
// Declare price and set it to 19.99
double price = 19.99;
// Print to the console with cout
std::cout << "$" << std::fixed << std::setprecision(2) << price << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
#include <iomanip>भूल जाना, ताकिsetwऔरsetprecisionundefined हों।- यह उम्मीद करना कि
setwpersist करेगा, जब यह सिर्फ अगले output item पर apply होता है। fixedके बिनाsetprecision(2)इस्तेमाल करना और दो decimal places की बजाय significant digits पाना।
Chapter Quiz — Complete all 8 topics to unlock
0/8 topics done
Complete these topics first: