home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / vc98 / crt / src / ostrdbl.cpp < prev    next >
C/C++ Source or Header  |  1998-06-17  |  2KB  |  62 lines

  1. /***
  2. * ostrdbl.cpp - definitions for ostream class operator<<(double) functions
  3. *
  4. *       Copyright (c) 1991-1997, Microsoft Corporation.  All rights reserved.
  5. *
  6. *Purpose:
  7. *       Contains the member function definitions for ostream operator<<(double).
  8. *
  9. *******************************************************************************/
  10.  
  11. #include <cruntime.h>
  12. #include <internal.h>
  13. #include <stdlib.h>
  14. #include <stdio.h>
  15. #include <float.h>
  16. #include <iostream.h>
  17. #pragma hdrstop
  18.  
  19. #pragma check_stack(on)         // large buffer(s)
  20.  
  21. ostream& ostream::operator<<(double f)
  22. {
  23. _WINSTATIC char obuffer[24];
  24. _WINSTATIC char fmt[8];
  25. _WINSTATIC char leader[4];
  26.     char * optr = obuffer;
  27.     int x = 0;
  28.  
  29.     // x_floatused nonzero indicates called for float, not double
  30.     unsigned int curprecision = (x_floatused) ? FLT_DIG : DBL_DIG;
  31.     x_floatused = 0;    // reset for next call
  32.  
  33.     curprecision = __min((unsigned)x_precision,curprecision);
  34.  
  35.     if (opfx()) {
  36.         if (x_flags & ios::showpos)
  37.             leader[x++] = '+';
  38.         if (x_flags & ios::showpoint)
  39.             leader[x++] = '#';  // show decimal and trailing zeros
  40.         leader[x] = '\0';
  41.         x = sprintf(fmt,"%%%s.%.0ug",leader,curprecision) - 1;
  42.         if ((x_flags & ios::floatfield)==ios::fixed)
  43.             fmt[x] = 'f';
  44.         else
  45.             {
  46.             if ((x_flags & ios::floatfield)==ios::scientific)
  47.                 fmt[x] = 'e';
  48.             if (x_flags & uppercase)
  49.                 fmt[x] = (char)toupper(fmt[x]);
  50.             }
  51.  
  52.         sprintf(optr,fmt,f);
  53.         x = 0;
  54.         if (*optr=='+' || *optr=='-')
  55.             leader[x++] = *(optr++);
  56.         leader[x] = '\0';
  57.         writepad(leader,optr);
  58.         osfx();
  59.         }
  60.     return *this;
  61. }
  62.