Temporarily set the locale to C when using ftoa, or the resulting string is subject to the locale and gets unexpected symbols (e.g. commas for the decimal dots while the function expects periods)

Tue, 19 Mar 2013 18:12:39 +0200

author
Santeri Piippo <crimsondusk64@gmail.com>
date
Tue, 19 Mar 2013 18:12:39 +0200
changeset 35
2b0569fee5ad
parent 34
d99006de6261
child 36
ac7779339b01

Temporarily set the locale to C when using ftoa, or the resulting string is subject to the locale and gets unexpected symbols (e.g. commas for the decimal dots while the function expects periods)

misc.cpp file | annotate | diff | comparison | revisions
--- a/misc.cpp	Tue Mar 19 17:21:51 2013 +0200
+++ b/misc.cpp	Tue Mar 19 18:12:39 2013 +0200
@@ -16,8 +16,9 @@
  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 
+#include <math.h>
+#include <locale.h>
 #include "common.h"
-#include <math.h>
 
 double getWordFloat (str& s, const ushort n) {
 	return atof ((s / " ")[n]);
@@ -67,6 +68,10 @@
 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 // =============================================================================
 str ftoa (double fCoord) {
+	// Disable the locale first so that the decimal point will not
+	// turn into anything weird (like commas)
+	setlocale (LC_NUMERIC, "C");
+	
 	str zRep = str::mkfmt ("%.3f", fCoord);
 	
 	// Remove trailing zeroes
@@ -78,6 +83,8 @@
 	if (zRep[~zRep - 1] == '.')
 		zRep -= 1;
 	
+	// Reset the locale
+	setlocale (LC_NUMERIC, "");
 	return zRep;
 }
 

mercurial