@@ -114,17 +114,20 @@ std::string valueToString(UInt value) {
114
114
115
115
#endif // # if defined(JSON_HAS_INT64)
116
116
117
- std::string valueToString (double value, bool useSpecialFloats) {
117
+ std::string valueToString (double value, bool useSpecialFloats, int precision ) {
118
118
// Allocate a buffer that is more than large enough to store the 16 digits of
119
119
// precision requested below.
120
120
char buffer[32 ];
121
121
int len = -1 ;
122
122
123
+ char formatString[6 ];
124
+ sprintf (formatString, " %%.%dg" , precision);
125
+
123
126
// Print into the buffer. We need not request the alternative representation
124
127
// that always has a decimal point because JSON doesn't distingish the
125
128
// concepts of reals and integers.
126
129
if (isfinite (value)) {
127
- len = snprintf (buffer, sizeof (buffer), " %.17g " , value);
130
+ len = snprintf (buffer, sizeof (buffer), formatString , value);
128
131
} else {
129
132
// IEEE standard states that NaN values will not compare to themselves
130
133
if (value != value) {
@@ -141,7 +144,7 @@ std::string valueToString(double value, bool useSpecialFloats) {
141
144
return buffer;
142
145
}
143
146
144
- std::string valueToString (double value) { return valueToString (value, false ); }
147
+ std::string valueToString (double value) { return valueToString (value, false , 17 ); }
145
148
146
149
std::string valueToString (bool value) { return value ? " true" : " false" ; }
147
150
@@ -882,7 +885,7 @@ void BuiltStyledStreamWriter::writeValue(Value const& value) {
882
885
pushValue (valueToString (value.asLargestUInt ()));
883
886
break ;
884
887
case realValue:
885
- pushValue (valueToString (value.asDouble (), useSpecialFloats_));
888
+ pushValue (valueToString (value.asDouble (), useSpecialFloats_, 17 ));
886
889
break ;
887
890
case stringValue:
888
891
{
0 commit comments