C++ के Format Specifiers
In this page:
#include <iomanip>
std::cout << std::fixed << std::setprecision(digits) << decimal_value;
std::cout << std::hex << integer_value; // or std::oct, std::dec
Floating-Point Formatting
fixed stream manipulator बहुत बड़ी या छोटी values के लिए scientific notation पर switch होने की बजाय floating-point numbers को standard decimal notation में print होने पर force करता है।
Exactly कितने digits decimal point के बाद दिखने हैं यह control करने के लिए इसे setprecision के साथ combine करें, जो money जैसी किसी भी चीज़ display करने के लिए essential है।
उदाहरण: Floating-Point Formatting
// Include std::cout and std::cin
#include <iostream>
#include <iomanip>
// Program execution starts in main()
int main() {
// Declare price and set it to 19.5
double price = 19.5;
// 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
Hexadecimal और Octal Formats
hex और oct manipulators बदलते हैं कि बाद के integers कैसे print होते हैं — hex उन्हें base-16 में दिखाता है और oct base-8 में — जबकि dec वापस normal base-10 पर switch करता है जिसकी आप उम्मीद करते हैं।
ये low-level code debug करते समय खासकर useful हैं, जहाँ एक value का hexadecimal form देखना bit patterns और memory addresses पढ़ना कहीं आसान बना देता है।
उदाहरण: Hexadecimal and Octal Formats
// Include std::cout and std::cin
#include <iostream>
// Program execution starts in main()
int main() {
// Declare value and set it to 255
int value = 255;
// Print to the console with cout
std::cout << std::hex << value << std::endl;
// Print to the console with cout
std::cout << std::oct << value << std::endl;
std::cout << std::dec << value << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Boolean Output Format
Default से C++ boolean values को raw integers 1 और 0 की तरह print करता है, जो एक human reader के लिए intended output में confusing हो सकता है।
boolalpha apply करना cout को इसकी जगह actual words true और false print कराता है, और noboolalpha बाद में ज़रूरत पड़ने पर numeric form पर वापस switch करता है।
उदाहरण: Boolean Output Format
#include <iostream>
int main() {
bool active = true;
std::cout << active << std::endl; // 1
std::cout << std::boolalpha << active << std::endl; // true
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Scientific Notation
scientific floating-point numbers को exponential notation इस्तेमाल करके format करता है, जैसे 150000000 की बजाय 1.5e+08, जो बहुत बड़ी या बहुत छोटी numbers को compact और एक नज़र में compare करना आसान रखता है।
यह scientific या engineering code में मायने रखता है जहाँ values same output में कई orders of magnitude फैल सकती हैं।
उदाहरण: Scientific Notation
// Include std::cout and std::cin
#include <iostream>
// Program execution starts in main()
int main() {
// Declare bigNumber and set it to 150000000.0
double bigNumber = 150000000.0;
// Print to the console with cout
std::cout << std::scientific << bigNumber << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Base Prefix Formatting
showbase एक visible prefix add करता है जो एक number का base identify करता है — hexadecimal के लिए 0x और octal के लिए एक leading 0 — ताकि एक reader को बाद में आने वाले digits interpret करने के लिए guess न करना पड़े।
यह hex या oct के साथ सबसे useful है, क्योंकि इसके बिना ff जैसी एक hex value आसानी से decimal समझी जा सकती है।
उदाहरण: Base Prefix Formatting
// Include std::cout and std::cin
#include <iostream>
// Program execution starts in main()
int main() {
// Declare value and set it to 255
int value = 255;
// Print to the console with cout
std::cout << std::showbase << std::hex << value << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
coutमें%dजैसेprintfstyle specifiers ढूंढना, जब C++ streams इसकी बजायhexऔरfixedजैसे manipulators इस्तेमाल करती हैं।fixedऔरsetprecisionset करना और उनके reset होने की उम्मीद करना, जब वे बाद के output के लिए effect में रहते हैं।- एक bool print करना और
trueकी उम्मीद करना, जब आपको 1 या 0 की बजाय text दिखाने के लिएboolalphaचाहिए।
Chapter Quiz — Complete all 8 topics to unlock
0/8 topics done
Complete these topics first: