Index: cpp/common/nonstd_math.cpp
===================================================================
--- cpp/common/nonstd_math.cpp	(revision 969)
+++ cpp/common/nonstd_math.cpp	(revision 970)
@@ -1,7 +1,12 @@
 // This file is a part of Framsticks SDK.  http://www.framsticks.com/
-// Copyright (C) 1999-2019  Maciej Komosinski and Szymon Ulatowski.
+// Copyright (C) 1999-2020  Maciej Komosinski and Szymon Ulatowski.
 // See LICENSE.txt for details.
 
 #include "nonstd_math.h"
+#ifdef USE_PRINTFLOAT_DRAGON4
+#include <PrintFloat/PrintFloat.h>
+#else
+#include <sstream>
+#endif
 
 RandomGenerator &rndGetInstance()
@@ -11,4 +16,35 @@
 }
 
+int doubleToString(double x, int precision, char *buffer, int bufferlen)
+{
+#ifdef USE_PRINTFLOAT_DRAGON4
+	return PrintFloat64(buffer, bufferlen, x,
+		((x < -1e17) || (x > 1e17) || ((x < 1e-4) && (x > -1e-4) && (x != 0.0)))
+		? PrintFloatFormat_Scientific : PrintFloatFormat_Positional,
+		precision); //http://www.ryanjuckett.com/programming/printing-floating-point-numbers/
+#else
+	char format[10];
+	sprintf(format, "%%.%dg", precision < 0 ? 17 : precision); //https://stackoverflow.com/questions/16839658/printf-width-specifier-to-maintain-precision-of-floating-point-value
+	return sprintf(buffer, format, x);
+#endif
+}
+
+
+double round(const double x, const int precision)
+{
+#ifdef USE_PRINTFLOAT_DRAGON4
+	char buffer[30];
+	doubleToString(x, precision, buffer, 30);
+	double rounded = strtod(buffer, NULL);
+#else
+	std::stringstream ss;
+	ss << std::fixed;
+	ss.precision(precision); // set the number of places after decimal
+	ss << x;
+	double rounded = stod(ss.str());
+#endif
+	//printf("%d  %20g \t %20g\n", precision, x, rounded); //for debugging
+	return rounded;
+}
 
 
Index: cpp/common/nonstd_math.h
===================================================================
--- cpp/common/nonstd_math.h	(revision 969)
+++ cpp/common/nonstd_math.h	(revision 970)
@@ -27,4 +27,17 @@
 inline void rndSetSeed(unsigned int seed) { rndGetInstance().setSeed(seed); }
 inline unsigned int rndRandomizeSeed() { return rndGetInstance().randomize(); }
+
+
+
+// precision==-1 for full precision, or positive values for the number of digits to print past the decimal point.
+// Allocated buffer with bufferlen=30 is OK.
+// Returns the number of chars actually used (not including the ending 0).
+int doubleToString(double x, int precision, char *buffer, int bufferlen);
+
+double round(const double x, const int precision);
+
+
+
+
 
 //floating point specific numbers
Index: cpp/frams/util/sstring-simple.cpp
===================================================================
--- cpp/frams/util/sstring-simple.cpp	(revision 969)
+++ cpp/frams/util/sstring-simple.cpp	(revision 970)
@@ -3,7 +3,6 @@
 #include "extvalue.h"
 #include <assert.h>
-#ifdef USE_PRINTFLOAT_DRAGON4
-#include <PrintFloat/PrintFloat.h>
-#endif
+#include <common/nonstd_math.h>
+
 
 void SString::initEmpty()
@@ -227,14 +226,7 @@
 SString SString::valueOf(double d)
 {
-#ifdef USE_PRINTFLOAT_DRAGON4
 	SString tmp;
 	char* here = tmp.directWrite(30);
-	tmp.endWrite(PrintFloat64(here, 30, d,
-		((d < -1e17) || (d > 1e17) || ((d < 1e-4) && (d > -1e-4) && (d != 0.0)))
-		? PrintFloatFormat_Scientific : PrintFloatFormat_Positional,
-		-1));//http://www.ryanjuckett.com/programming/printing-floating-point-numbers/
-#else
-	SString tmp = SString::sprintf("%.17g", d); //https://stackoverflow.com/questions/16839658/printf-width-specifier-to-maintain-precision-of-floating-point-value
-#endif
+	tmp.endWrite(doubleToString(d, -1, here, 30));
 	if ((!strchr(tmp.c_str(), '.')) && (!strchr(tmp.c_str(), 'e'))) tmp += ".0";
 	return tmp;
Index: cpp/frams/util/sstring.cpp
===================================================================
--- cpp/frams/util/sstring.cpp	(revision 969)
+++ cpp/frams/util/sstring.cpp	(revision 970)
@@ -1,4 +1,4 @@
 // This file is a part of Framsticks SDK.  http://www.framsticks.com/
-// Copyright (C) 1999-2015  Maciej Komosinski and Szymon Ulatowski.
+// Copyright (C) 1999-2020  Maciej Komosinski and Szymon Ulatowski.
 // See LICENSE.txt for details.
 
@@ -23,7 +23,5 @@
 #include "extvalue.h"
 #include <assert.h>
-#ifdef USE_PRINTFLOAT_DRAGON4
-#include <PrintFloat/PrintFloat.h>
-#endif
+#include <common/nonstd_math.h>
 
 #ifdef MULTITHREADED
@@ -367,14 +365,7 @@
 SString SString::valueOf(double d)
 {
-#ifdef USE_PRINTFLOAT_DRAGON4
 	SString tmp;
 	char* here = tmp.directWrite(30);
-	tmp.endWrite(PrintFloat64(here, 30, d,
-		((d < -1e17) || (d > 1e17) || ((d < 1e-4) && (d > -1e-4) && (d != 0.0)))
-		? PrintFloatFormat_Scientific : PrintFloatFormat_Positional,
-		-1));//http://www.ryanjuckett.com/programming/printing-floating-point-numbers/
-#else
-	SString tmp = SString::sprintf("%.17g", d); //https://stackoverflow.com/questions/16839658/printf-width-specifier-to-maintain-precision-of-floating-point-value
-#endif
+	tmp.endWrite(doubleToString(d, -1, here, 30));
 	if ((!strchr(tmp.c_str(), '.')) && (!strchr(tmp.c_str(), 'e'))) tmp += ".0";
 	return tmp;
