From a89dec25adcb3566457d36ba50d38777268f4cd1 Mon Sep 17 00:00:00 2001 From: Stefano Fiorentino Date: Fri, 12 Feb 2016 11:28:50 +0100 Subject: [PATCH] address the double to json format accounting for integer side number of digits --- src/lib_json/json_writer.cpp | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/lib_json/json_writer.cpp b/src/lib_json/json_writer.cpp index 045dc44..bce273a 100644 --- a/src/lib_json/json_writer.cpp +++ b/src/lib_json/json_writer.cpp @@ -141,8 +141,11 @@ std::string valueToString(double value, bool useSpecialFloats, unsigned int prec char buffer[32]; int len = -1; + int length = (value==0.0)?1:(int)(std::floor(std::log10(std::abs(value)))+1); + if (length<1) length=1; + char formatString[6]; - sprintf(formatString, "%%.%dg", precision); + sprintf(formatString, "%%.%dg", precision-length); // Print into the buffer. We need not request the alternative representation // that always has a decimal point because JSON doesn't distingish the