简单打印不同格式的数字
How to do it...
#include <iostream> #include <iomanip> using namespace std;class format_guard { decltype(cout.flags()) f {cout.flags()}; public: ~format_guard() { cout.flags(f); } };template <typename T> struct scientific_type { T value; explicit scientific_type(T val) : value{val} {} };template <typename T> ostream& operator<<(ostream &os, const scientific_type<T> &w) { format_guard _; os << scientific << uppercase << showpos; return os << w.value; }int main() { { format_guard _; cout << hex << scientific << showbase << uppercase; cout << "Numbers with special formatting:\n"; cout << 0x123abc << '\n'; cout << 0.123456789 << '\n'; }cout << "Same numbers, but normal formatting again:\n"; cout << 0x123abc << '\n'; cout << 0.123456789 << '\n';cout << "Mixed formatting: " << 123.0 << " " << scientific_type{123.0} << " " << 123.456 << '\n'; }$ ./pretty_print_on_the_fly Numbers with special formatting: 0X123ABC 1.234568E-01 Same numbers, but normal formatting again: 1194684 0.123457 Mixed formatting: 123 +1.230000E+02 123.456
Last updated