Skip to content

Commit

Permalink
parsing and serialization does not produce wrong value for systems th…
Browse files Browse the repository at this point in the history
…at use ',' as decimal seperator anymore
  • Loading branch information
Stratege committed Oct 13, 2016
1 parent 12256e9 commit 1170a64
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 8 deletions.
4 changes: 2 additions & 2 deletions ink-engine-runtime/SimpleJson.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions ink-engine-runtime/Value.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -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;
Expand Down
6 changes: 5 additions & 1 deletion inklecate/ParsedHierarchy/Number.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions tests/Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()]
Expand Down Expand Up @@ -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()]
Expand Down Expand Up @@ -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";
Expand Down

0 comments on commit 1170a64

Please sign in to comment.