From 18f93b4eb6af25eec82cdfdbe919fbdd17117258 Mon Sep 17 00:00:00 2001 From: Benoit Blanchon Date: Sat, 20 Sep 2014 18:44:10 +0200 Subject: [PATCH] Fixed issue #22 --- JsonGenerator/Print.cpp | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/JsonGenerator/Print.cpp b/JsonGenerator/Print.cpp index daa26b43..f90cd13c 100644 --- a/JsonGenerator/Print.cpp +++ b/JsonGenerator/Print.cpp @@ -5,9 +5,9 @@ #ifndef ARDUINO -#include "Print.h" -#include - +#include "Print.h" +#include // for sprintf, strchr and strcat + size_t Print::print(const char s[]) { size_t n = 0; @@ -18,10 +18,26 @@ size_t Print::print(const char s[]) return n; } +static inline void ensureStringContainsPoint(char* s) +{ + // Ensures that the decimal point is present. + // For example, we don't want "0" but "0.0". + // Otherwise, the value would be considered as an integer by some parsers + // See issue #22 + + if (!strchr(s, '.')) + strcat(s, ".0"); +} + size_t Print::print(double value, int digits) { char tmp[32]; + sprintf(tmp, "%.*lg", digits+1, value); + + if (digits>0) + ensureStringContainsPoint(tmp); + return print(tmp); }