Deitel & Associates, Inc. Logo

Back to www.deitel.com
digg.png delicious.png blinkit.png furl.png
C++ How to Program, 5/e

ISBN:
0-13-185757-6
© 2005
pages: 1500
Buy the Book!
Amazon logo
InformIT logo

This tutorial introduces ANSI/ISO C++'s stream formatting capabilties, including stream manipulators, justification, padding, integer formats, floating-point number formats, uppercase/lowercase control, formatting booleans as strings, and setting and restoring stream format states. This tutorial is intented for students and developers who are familiar with basic C++ input and output techniques using cin and cout. The techniques shown here can also be applied to file-based streams or other streams that extend the standard C++ stream class hierarchy.

Download the code examples for this tutorial.

[Note: This tutorial is an excerpt (Section 15.7) of Chapter 15, Stream Input/Output, from our textbook C++ How to Program, 5/e. These tutorials may refer to other chapters or sections of the book that are not included here. Permission Information: Deitel, Harvey M. and Paul J., C++ HOW TO PROGRAM, ©2005, pp.787-797. Electronically reproduced by permission of Pearson Education, Inc., Upper Saddle River, New Jersey.]

15.7 Stream Format States and Stream Manipulators (Continued)

15.7.5 Floating-Point Numbers; Scientific and Fixed Notation (scientific, fixed)

Stream manipulators scientific and fixed control the output format of floating-point numbers. Stream manipulator scientific forces the output of a floating-point number to display in scientific format. Stream manipulator fixed forces a floating-point number to display a specific number of digits (as specified by member function precision or stream manipulator setprecision) to the right of the decimal point. Without using another manipulator, the floating-point-number value determines the output format.

Figure 15.18 demonstrates displaying floating-point numbers in fixed and scientific formats using stream manipulators scientific (line 21) and fixed (line 25). The exponent format in scientific notation might differ across different compilers.

 1  // Fig. 15.18: Fig15_18.cpp
 2  // Displaying floating-point values in system default,
 3  // scientific and fixed formats.
 4  #include <iostream>
 5  using std::cout;
 6  using std::endl;
 7  using std::fixed;
 8  using std::scientific;
 9
10  int main()
11  {
12     double x = 0.001234567;
13     double y = 1.946e9;
14
15     // display x and y in default format
16     cout << "Displayed in default format:" << endl
17        << x << '\t' << y << endl;
18
19     // display x and y in scientific format
20     cout << "\nDisplayed in scientific format:" << endl
21        << scientific << x << '\t' << y << endl;
22
23     // display x and y in fixed format
24     cout << "\nDisplayed in fixed format:" << endl
25        << fixed << x << '\t' << y << endl;
26     return 0;
27  } // end main
 Fig. 15.18  Floating-point values displayed in default, scientific and fixed formats.

 Displayed in default format:
 0.00123457      1.946e+009

 Displayed in scientific format:
 1.234567e-003   1.946000e+009

 Displayed in fixed format:
 0.001235        1946000000.000000

Other sections in this tutorial:
Introduction to Stream Format States and Stream Manipulators
15.7.1 Trailing Zeros and Decimal Points (showpoint)
15.7.2 Justification (left, right and internal)
15.7.3 Padding (fill, setfill)
15.7.4 Integral Stream Base (dec, oct, hex, showbase)
15.7.5 Floating-Point Numbers; Scientific and Fixed Notation (scientific, fixed) (You are here)
15.7.6 Uppercase/Lowercase Control (uppercase).
15.7.7 Specifying Boolean Format (boolalpha)
15.7.8 Setting and Resetting the Format State via Member Function flags

Tutorial Index