home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / listings / v_11_07 / 1107104a < prev    next >
Text File  |  1993-05-04  |  898b  |  43 lines

  1. // float.cpp:   Format real numbers
  2. #include <iostream.h>
  3.  
  4. main()
  5. {
  6.     float x = 12345.6789, y = 12345;
  7.     
  8.     cout << x << ' ' << y << '\n';
  9.     
  10.     // Show two decimals
  11.     cout.precision(2);
  12.     cout << x << ' ' << y << '\n';
  13.     
  14.     // Show trailing zeroes
  15.     cout.setf(ios::showpoint);
  16.     cout << x << ' ' << y << '\n';
  17.     
  18.     // Show sign
  19.     cout.setf(ios::showpos);
  20.     cout << x << ' ' << y << '\n';
  21.  
  22.     // Return sign and precision to defaults
  23.     cout.unsetf(ios::showpos);
  24.     cout.precision(0);
  25.     
  26.     // Use scientific notation
  27.     cout.setf(ios::scientific,ios::floatfield);
  28.     float z = 1234567890.123456;
  29.     cout << z << '\n';
  30.     cout.setf(ios::uppercase);
  31.     cout << z << '\n';
  32.     return 0;
  33. }
  34.  
  35. // Output
  36. // 12345.678711 12345
  37. // 12345.68 12345
  38. // 12345.68 12345.00
  39. // +12345.68 +12345.00
  40. // 1.234568e+09
  41. // 1.234568E+09
  42.  
  43.