Skip to content

Commit 039a6e3

Browse files
committed
Create format string with sprintf.
For now use hardcoded precision '17' for now
1 parent 9c17e61 commit 039a6e3

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
@@ -133,17 +133,20 @@ std::string valueToString(UInt value) {
133133

134134
#endif // # if defined(JSON_HAS_INT64)
135135

136-
std::string valueToString(double value, bool useSpecialFloats) {
136+
std::string valueToString(double value, bool useSpecialFloats, int precision) {
137137
// Allocate a buffer that is more than large enough to store the 16 digits of
138138
// precision requested below.
139139
char buffer[32];
140140
int len = -1;
141141

142+
char formatString[6];
143+
sprintf(formatString, "%%.%dg", precision);
144+
142145
// Print into the buffer. We need not request the alternative representation
143146
// that always has a decimal point because JSON doesn't distingish the
144147
// concepts of reals and integers.
145148
if (isfinite(value)) {
146-
len = snprintf(buffer, sizeof(buffer), "%.17g", value);
149+
len = snprintf(buffer, sizeof(buffer), formatString, value);
147150
} else {
148151
// IEEE standard states that NaN values will not compare to themselves
149152
if (value != value) {
@@ -160,7 +163,7 @@ std::string valueToString(double value, bool useSpecialFloats) {
160163
return buffer;
161164
}
162165

163-
std::string valueToString(double value) { return valueToString(value, false); }
166+
std::string valueToString(double value) { return valueToString(value, false, 17); }
164167

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

@@ -906,7 +909,7 @@ void BuiltStyledStreamWriter::writeValue(Value const& value) {
906909
pushValue(valueToString(value.asLargestUInt()));
907910
break;
908911
case realValue:
909-
pushValue(valueToString(value.asDouble(), useSpecialFloats_));
912+
pushValue(valueToString(value.asDouble(), useSpecialFloats_, 17));
910913
break;
911914
case stringValue:
912915
{

0 commit comments

Comments
 (0)