#include <formatnum.h>
Public Types | |
| enum | { GroupThousands = 0x1, SignificantDigits = 0x2 } |
Public Member Functions | |
| FormattedNumber (double, unsigned int _precision, unsigned int _flags) | |
| double | getRoundedValue () const |
| double | getValue () const |
Private Attributes | |
| unsigned int | flags |
| unsigned int | precision |
| double | value |
Friends | |
| std::ostream & | operator<< (std::ostream &out, const FormattedNumber &num) |
|
|
Definition at line 23 of file formatnum.h. 00024 {
00025 GroupThousands = 0x1,
00026 SignificantDigits = 0x2,
00027 };
|
|
||||||||||||||||
|
Definition at line 16 of file formatnum.cpp.
|
|
|
Definition at line 32 of file formatnum.cpp. References flags, pow(), precision, SignificantDigits, and value. 00033 {
00034 if (flags & SignificantDigits)
00035 {
00036 if (value == 0.0)
00037 {
00038 return 0.0;
00039 }
00040 else
00041 {
00042 double m = pow(10.0, floor(log10(fabs(value))) - precision + 1);
00043 return floor(value / m + 0.5) * m;
00044 }
00045 }
00046 else
00047 {
00048 return value;
00049 }
00050 }
|
|
|
Definition at line 26 of file formatnum.cpp. References value. 00027 {
00028 return value;
00029 }
|
|
||||||||||||
|
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 }
|
|
|
Definition at line 34 of file formatnum.h. Referenced by getRoundedValue(). |
|
|
Definition at line 33 of file formatnum.h. Referenced by getRoundedValue(). |
|
|
Definition at line 32 of file formatnum.h. Referenced by getRoundedValue(), and getValue(). |
1.4.1