forked from CelestiaProject/Celestia
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathformatnum.cpp
More file actions
142 lines (123 loc) · 3.37 KB
/
formatnum.cpp
File metadata and controls
142 lines (123 loc) · 3.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
// formatnum.cpp
//
// Copyright (C) 2003, Chris Laurel <claurel@shatters.net>
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
#include <cmath>
#include <cstdio>
#include <cstring>
#include <climits>
#include "formatnum.h"
// HACK: MS Visual C++ has _snprintf declared in stdio.h but not snprintf
#ifdef _WIN32
#define snprintf _snprintf
#endif
FormattedNumber::FormattedNumber(double v,
unsigned int _precision,
unsigned int _flags) :
value(v),
precision(_precision),
flags(_flags)
{
}
double FormattedNumber::getValue() const
{
return value;
}
double FormattedNumber::getRoundedValue() const
{
if (flags & SignificantDigits)
{
if (value == 0.0)
return 0.0;
double m = pow(10.0, floor(log10(fabs(value))) - precision + 1);
return floor(value / m + 0.5) * m;
}
else
{
return value;
}
}
std::ostream& operator<<(std::ostream& out, const FormattedNumber& num)
{
char fmt[32];
char buf[32];
char obuf[64];
int fmtPrecision;
double value = num.getRoundedValue();
char *decimal_point = localeconv()->decimal_point;
char *thousands_sep = localeconv()->thousands_sep;
char *grouping = localeconv()->grouping;
memset(obuf, 0, sizeof(obuf));
if (num.flags & FormattedNumber::SignificantDigits)
{
if (value == 0.0)
{
fmtPrecision = 5;
}
else
{
fmtPrecision = (int) log10(fabs(value)) - num.precision + 1;
if (fabs(value) < 1.0)
fmtPrecision--;
fmtPrecision = fmtPrecision > 0 ? 0 : -fmtPrecision;
}
}
else
{
fmtPrecision = num.precision;
}
snprintf(fmt, sizeof(fmt)/sizeof(char), "%%.%df", fmtPrecision);
snprintf(buf, sizeof(buf)/sizeof(char), fmt, value);
if (num.flags & FormattedNumber::GroupThousands)
{
const char* decimalPosition = strstr(buf, decimal_point);
int j = sizeof(obuf) - 1;
int i = strlen(buf);
int digitCount = 0;
if (decimalPosition != nullptr)
{
int len = strlen(decimalPosition);
j -= len;
i -= len;
memcpy(obuf + j, decimalPosition, len);
--i;
--j;
}
const char *g = grouping;
bool does_grouping = *g != 0;
while (i >= 0)
{
if (isdigit(buf[i]))
{
if (does_grouping && *g != CHAR_MAX)
{
if (digitCount == *g)
{
const char *c, *ts = thousands_sep;
for (c = ts + strlen(ts) - 1; c >= ts; c--)
{
obuf[j] = *c;
j--;
}
if (*(g+1) != 0) g += 1;
digitCount = 0;
}
}
digitCount++;
}
obuf[j] = buf[i];
j--;
i--;
}
out << (obuf + (j + 1));
}
else
{
out << buf;
}
return out;
}