diff --git a/libstuff/libstuff.cpp b/libstuff/libstuff.cpp index 692f0d9ac..466ddc54f 100644 --- a/libstuff/libstuff.cpp +++ b/libstuff/libstuff.cpp @@ -1191,8 +1191,13 @@ extern const char* _SParseJSONValue(const char* ptr, const char* end, string& va string SToJSON(const string& value, const bool forceString) { // Is it an integer? - if (SToStr(SToInt64(value.c_str())) == value) + if (SToStr(SToInt64(value.c_str())) == value) { return value; + } + // Is it a float? + if (SToStr(SToFloat(value.c_str())) == value) { + return value; + } // Is it boolean? if (SIEquals(value, "true")) diff --git a/libstuff/libstuff.h b/libstuff/libstuff.h index a2483784a..f85a4fbeb 100644 --- a/libstuff/libstuff.h +++ b/libstuff/libstuff.h @@ -26,6 +26,7 @@ #include #include #include +#include #include #include #include @@ -378,7 +379,7 @@ string SHexStringFromBase32(const string& buffer); // **NOTE: Use 'ostringstream' because 'stringstream' leaks on VS2005 template inline string SToStr(const T& t) { ostringstream ss; - ss << t; + ss << fixed << showpoint << setprecision(6) << t; return ss.str(); } diff --git a/test/tests/LibStuffTest.cpp b/test/tests/LibStuffTest.cpp index 99ca23aba..390abcfaf 100644 --- a/test/tests/LibStuffTest.cpp +++ b/test/tests/LibStuffTest.cpp @@ -62,9 +62,15 @@ struct LibStuff : tpunit::TestFixture { void testJSON() { // Floating point value tests - ASSERT_EQUAL(SToJSON("{\"imAFloat\":1.2}"), "{\"imAFloat\":1.2}"); + ASSERT_EQUAL(SToJSON("{\"imAFloat\":0.0000}"), "{\"imAFloat\":0.0000}"); ASSERT_EQUAL(SToJSON("{\"imAFloat\":-0.23456789}"), "{\"imAFloat\":-0.23456789}"); ASSERT_EQUAL(SToJSON("{\"imAFloat\":-123456789.23456789}"), "{\"imAFloat\":-123456789.23456789}"); + ASSERT_EQUAL(SToJSON("{\"object\":{\"imAFloat\":1.00}}"), "{\"object\":{\"imAFloat\":1.00}}"); + + STable testFloats; + testFloats["imAFloat"] = (double)0.000; + string returnVal = SComposeJSONObject(testFloats); + ASSERT_EQUAL(returnVal, "{\"imAFloat\":0.000000}"); // Scientific notation tests ASSERT_EQUAL(SToJSON("{\"science\":1.5e-8}"), "{\"science\":1.5e-8}");