From 1170a64ecba39000151c7118a80c0288db66d9b1 Mon Sep 17 00:00:00 2001 From: Christian Fania Date: Thu, 13 Oct 2016 21:03:25 +0200 Subject: [PATCH] parsing and serialization does not produce wrong value for systems that use ',' as decimal seperator anymore --- ink-engine-runtime/SimpleJson.cs | 4 ++-- ink-engine-runtime/Value.cs | 4 ++-- inklecate/ParsedHierarchy/Number.cs | 6 +++++- tests/Tests.cs | 6 +++--- 4 files changed, 12 insertions(+), 8 deletions(-) diff --git a/ink-engine-runtime/SimpleJson.cs b/ink-engine-runtime/SimpleJson.cs index cd5f2418..8bc17e94 100644 --- a/ink-engine-runtime/SimpleJson.cs +++ b/ink-engine-runtime/SimpleJson.cs @@ -185,7 +185,7 @@ object ReadNumber () if (isFloat) { float f; - if (float.TryParse (numStr, out f)) { + if (float.TryParse (numStr, System.Globalization.NumberStyles.Float, System.Globalization.CultureInfo.InvariantCulture, out f)) { return f; } } else { @@ -264,7 +264,7 @@ void WriteObject (object obj) if (obj is int) { _sb.Append ((int)obj); } else if (obj is float) { - string floatStr = obj.ToString (); + string floatStr = ((float)obj).ToString(System.Globalization.CultureInfo.InvariantCulture); _sb.Append (floatStr); if (!floatStr.Contains (".")) _sb.Append (".0"); } else if( obj is bool) { diff --git a/ink-engine-runtime/Value.cs b/ink-engine-runtime/Value.cs index 8d30a275..2e8ff1fb 100644 --- a/ink-engine-runtime/Value.cs +++ b/ink-engine-runtime/Value.cs @@ -138,7 +138,7 @@ public override Value Cast(ValueType newType) } if (newType == ValueType.String) { - return new StringValue("" + this.value); + return new StringValue("" + this.value.ToString(System.Globalization.CultureInfo.InvariantCulture)); } throw new System.Exception ("Unexpected type cast of Value to new ValueType"); @@ -191,7 +191,7 @@ public override Value Cast(ValueType newType) if (newType == ValueType.Float) { float parsedFloat; - if (float.TryParse (value, out parsedFloat)) { + if (float.TryParse (value, System.Globalization.NumberStyles.Float ,System.Globalization.CultureInfo.InvariantCulture, out parsedFloat)) { return new FloatValue (parsedFloat); } else { return null; diff --git a/inklecate/ParsedHierarchy/Number.cs b/inklecate/ParsedHierarchy/Number.cs index 67863cd2..e4120e36 100644 --- a/inklecate/ParsedHierarchy/Number.cs +++ b/inklecate/ParsedHierarchy/Number.cs @@ -25,7 +25,11 @@ public override void GenerateIntoContainer (Runtime.Container container) public override string ToString () { - return value.ToString (); + if (value is float) { + return ((float)value).ToString(System.Globalization.CultureInfo.InvariantCulture); + } else { + return value.ToString(); + } } // Equals override necessary in order to check for CONST multiple definition equality diff --git a/tests/Tests.cs b/tests/Tests.cs index 704b7d85..d61cb797 100644 --- a/tests/Tests.cs +++ b/tests/Tests.cs @@ -90,7 +90,7 @@ public void TestArithmetic() { 2 * (5-1) } "); - Assert.AreEqual("36\n2\n3\n2\n2.333333\n8\n8\n", story.ContinueMaximally()); + Assert.AreEqual("36\n2\n3\n2\n2"+System.Globalization.NumberFormatInfo.CurrentInfo.NumberDecimalSeparator+"333333\n8\n8\n", story.ContinueMaximally()); } [Test()] @@ -327,7 +327,7 @@ one and a half ({num}) ->-> "); - Assert.AreEqual("one (1)\none and a half (1.5)\ntwo (2)\nthree (3)\n", story.ContinueMaximally()); + Assert.AreEqual("one (1)\none and a half (1"+ System.Globalization.NumberFormatInfo.CurrentInfo.NumberDecimalSeparator+"5)\ntwo (2)\nthree (3)\n", story.ContinueMaximally()); } [Test()] @@ -1996,7 +1996,7 @@ public void TestVariableGetSetAPI() story.variablesState["x"] = 8.5f; story.ChooseChoiceIndex(0); - Assert.AreEqual("8.5\n", story.ContinueMaximally()); + Assert.AreEqual("8"+ System.Globalization.NumberFormatInfo.CurrentInfo.NumberDecimalSeparator+"5\n", story.ContinueMaximally()); Assert.AreEqual(8.5f, story.variablesState["x"]); story.variablesState["x"] = "a string";