@@ -133,17 +133,20 @@ std::string valueToString(UInt value) {
133
133
134
134
#endif // # if defined(JSON_HAS_INT64)
135
135
136
- std::string valueToString (double value, bool useSpecialFloats) {
136
+ std::string valueToString (double value, bool useSpecialFloats, int precision ) {
137
137
// Allocate a buffer that is more than large enough to store the 16 digits of
138
138
// precision requested below.
139
139
char buffer[32 ];
140
140
int len = -1 ;
141
141
142
+ char formatString[6 ];
143
+ sprintf (formatString, " %%.%dg" , precision);
144
+
142
145
// Print into the buffer. We need not request the alternative representation
143
146
// that always has a decimal point because JSON doesn't distingish the
144
147
// concepts of reals and integers.
145
148
if (isfinite (value)) {
146
- len = snprintf (buffer, sizeof (buffer), " %.17g " , value);
149
+ len = snprintf (buffer, sizeof (buffer), formatString , value);
147
150
} else {
148
151
// IEEE standard states that NaN values will not compare to themselves
149
152
if (value != value) {
@@ -160,7 +163,7 @@ std::string valueToString(double value, bool useSpecialFloats) {
160
163
return buffer;
161
164
}
162
165
163
- std::string valueToString (double value) { return valueToString (value, false ); }
166
+ std::string valueToString (double value) { return valueToString (value, false , 17 ); }
164
167
165
168
std::string valueToString (bool value) { return value ? " true" : " false" ; }
166
169
@@ -906,7 +909,7 @@ void BuiltStyledStreamWriter::writeValue(Value const& value) {
906
909
pushValue (valueToString (value.asLargestUInt ()));
907
910
break ;
908
911
case realValue:
909
- pushValue (valueToString (value.asDouble (), useSpecialFloats_));
912
+ pushValue (valueToString (value.asDouble (), useSpecialFloats_, 17 ));
910
913
break ;
911
914
case stringValue:
912
915
{
0 commit comments