Skip to content

Commit 2f9a6a6

Browse files
bknechtcdunn2001
authored andcommitted
Create format string with sprintf.
For now use hardcoded precision '17' for now
1 parent 26159b9 commit 2f9a6a6

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

src/lib_json/json_writer.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -114,17 +114,20 @@ std::string valueToString(UInt value) {
114114

115115
#endif // # if defined(JSON_HAS_INT64)
116116

117-
std::string valueToString(double value, bool useSpecialFloats) {
117+
std::string valueToString(double value, bool useSpecialFloats, int precision) {
118118
// Allocate a buffer that is more than large enough to store the 16 digits of
119119
// precision requested below.
120120
char buffer[32];
121121
int len = -1;
122122

123+
char formatString[6];
124+
sprintf(formatString, "%%.%dg", precision);
125+
123126
// Print into the buffer. We need not request the alternative representation
124127
// that always has a decimal point because JSON doesn't distingish the
125128
// concepts of reals and integers.
126129
if (isfinite(value)) {
127-
len = snprintf(buffer, sizeof(buffer), "%.17g", value);
130+
len = snprintf(buffer, sizeof(buffer), formatString, value);
128131
} else {
129132
// IEEE standard states that NaN values will not compare to themselves
130133
if (value != value) {
@@ -141,7 +144,7 @@ std::string valueToString(double value, bool useSpecialFloats) {
141144
return buffer;
142145
}
143146

144-
std::string valueToString(double value) { return valueToString(value, false); }
147+
std::string valueToString(double value) { return valueToString(value, false, 17); }
145148

146149
std::string valueToString(bool value) { return value ? "true" : "false"; }
147150

@@ -882,7 +885,7 @@ void BuiltStyledStreamWriter::writeValue(Value const& value) {
882885
pushValue(valueToString(value.asLargestUInt()));
883886
break;
884887
case realValue:
885-
pushValue(valueToString(value.asDouble(), useSpecialFloats_));
888+
pushValue(valueToString(value.asDouble(), useSpecialFloats_, 17));
886889
break;
887890
case stringValue:
888891
{

0 commit comments

Comments
 (0)