Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members

formatnum.cpp File Reference

#include <cmath>
#include <cstdio>
#include <string>
#include "formatnum.h"

Include dependency graph for formatnum.cpp:

Go to the source code of this file.

Functions

std::ostream & operator<< (std::ostream &out, const FormattedNumber &num)


Function Documentation

std::ostream& operator<< std::ostream &  out,
const FormattedNumber num
 

Definition at line 53 of file formatnum.cpp.

00054 {
00055     char fmt[32];
00056     char buf[32];
00057     char obuf[64];
00058     double value = num.getRoundedValue();
00059     char *decimal_point = localeconv()->decimal_point;
00060     char *thousands_sep = localeconv()->thousands_sep;
00061     char *grouping = localeconv()->grouping;
00062 
00063     memset(obuf, 0, sizeof(obuf));
00064 
00065     if (num.flags & FormattedNumber::SignificantDigits)
00066     {
00067         if (value == 0.0)
00068         {
00069             sprintf(fmt, "%%.%df", 5);
00070         }
00071         else
00072         {
00073             int fmtPrecision = (int) log10(fabs(value)) - num.precision + 1;
00074             if (fabs(value) < 1.0)
00075                 fmtPrecision--;
00076             sprintf(fmt, "%%.%df", fmtPrecision > 0 ? 0 : -fmtPrecision);
00077         }
00078     }
00079     else
00080     {
00081         sprintf(fmt, "%%.%df", num.precision);
00082     }
00083 
00084     sprintf(buf, fmt, value);
00085 
00086     if (num.flags & FormattedNumber::GroupThousands)
00087     {
00088         const char* decimalPosition = strstr(buf, decimal_point);
00089         int j = sizeof(obuf) - 1;
00090         int i = strlen(buf);
00091         int digitCount = 0;
00092         if (decimalPosition != NULL)
00093         {
00094             int len = strlen(decimalPosition);
00095             j -= len;
00096             i -= len;
00097             memcpy(obuf + j, decimalPosition, len);
00098             --i;
00099             --j;
00100         }
00101         
00102         const char *g = grouping;
00103         bool does_grouping = *g != 0;
00104         while (i >= 0)
00105         {
00106             if (isdigit(buf[i]))
00107             {
00108                 if (does_grouping && *g != CHAR_MAX)
00109                 {
00110                     if (digitCount == *g)
00111                     {
00112                         const char *c, *ts = thousands_sep;
00113                         for (c = ts + strlen(ts) - 1; c >= ts; c--)
00114                         {
00115                             obuf[j] = *c;
00116                             j--;
00117                         }
00118                         if (*(g+1) != 0) g += 1;
00119                         digitCount = 0;
00120                     }
00121                 }
00122                 digitCount++;
00123             }
00124 
00125             obuf[j] = buf[i];
00126 
00127             j--;
00128             i--;
00129         }
00130 
00131         out << (obuf + (j + 1));
00132     }
00133     else
00134     {
00135         out << buf;
00136     }
00137 
00138     return out;
00139 }


Generated on Sat Jan 14 22:32:59 2006 for Celestia by  doxygen 1.4.1