Ne.app NeANT
The Ne.app NeANT User module of the NeANT distribution.
num_put.cpp
Go to the documentation of this file.
1
29#include <iostream> // for cout, endl
30#include <iterator> // for ostreambuf_iterator
31#include <locale> // for time_put
32
33#include <examples.h>
34
35
36// custom numeric punctuation facet
37struct Punct: std::numpunct<char>
38{
39 char do_decimal_point () const {
40 return ';';
41 }
42
44 return "numeric formatting";
45 }
46
47 char do_thousands_sep () const {
48 return ':';
49 }
50
51 std::string do_grouping () const {
52 return "\1\2\3";
53 }
54};
55
56
57int main ()
58{
59 // construct a custom punctuation facet
60 std::numpunct<char> *punct = new Punct;
61
62 // construct a locale containing the custom facet
63 const std::locale loc (std::cout.getloc (), punct);
64
65 // imbue combined locale in cout
66 std::cout.imbue (loc);
67
68 // retrieve the standard numeric formatting facet
69 const std::num_put<char> &nput =
70 std::use_facet<std::num_put<char> >(std::cout.getloc ());
71
72 // obtain a stream buffer iterator operating on cout's buffer
73 std::ostreambuf_iterator<char> it = std::cout.rdbuf ();
74
75 // write out bool values as strings
76 std::cout.setf (std::ios::boolalpha);
77 *nput.put (it, std::cout, ' ', true) = '\n';
78
79 // set the field width and justification for the next operation
80 std::cout.width (18);
81 std::cout.setf (std::ios::fixed | std::ios::right);
82
83 const double Pi = 3.14159265358979323846;
84
85 // truncate Pi to an integer and write it out, padded on the left with periods
86 *nput.put (it, std::cout, '.', long (Pi)) = '\n';
87
88 // write out a number in hex notation using capital letters
89 std::cerr.width (13);
90 std::cerr.setf (std::ios::right | std::ios::uppercase);
91 std::cerr.setf (std::ios::hex, std::ios::basefield);
92 *nput.put (it, std::cerr, ' ', 3735928559UL) = '\n';
93
94 std::cout.width (18);
95 std::cout.setf (std::ios::internal, std::ios::adjustfield);
96 std::cout.setf (std::ios::showpos);
97
98 // write Pi out padded after the sign with spaces
99 *nput.put (it, std::cout, ' ', Pi) = '\n';
100
101 // write Pi out again, multiplied by a large number (to see grouping)
102 *nput.put (it, std::cout, ' ', Pi * 1000000) = '\n';
103
104 // and again, in scientific notation
105 std::cout.precision (11);
106 std::cout.setf (std::ios::scientific, std::ios::floatfield);
107 *nput.put (it, std::cout, ' ', Pi * 1000000) = '\n';
108
109 return 0;
110}
const char punct[]
#define loc
Definition 17.names.cpp:492
int main()
Definition num_put.cpp:57
char do_decimal_point() const
Definition num_put.cpp:39
std::string do_grouping() const
Definition num_put.cpp:51
char do_thousands_sep() const
Definition num_put.cpp:47
string_type do_truename() const
Definition num_put.cpp:43
Base::string_type string_type