Skip to content

Commit 341cc27

Browse files
committed
Fixed issue with non-invariant number formats occuring on EU locales (french, german or similar) using , instead of . as decimal separators.
Closes issue #305
1 parent 579985f commit 341cc27

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

Parse.Test/ConversionTests.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,22 @@ struct DummyValueTypeB { }
1818

1919
[TestMethod]
2020
public void TestToWithConstructedNullableNonPrimitive() => Assert.ThrowsException<InvalidCastException>(() => Conversion.To<DummyValueTypeA?>(new DummyValueTypeB { }));
21+
22+
23+
24+
[TestMethod]
25+
public void TestConvertToFloatUsingNonInvariantNumberFormat()
26+
{
27+
try
28+
{
29+
float inputValue = 1234.56f;
30+
string jsonEncoded = Common.Internal.Json.Encode(inputValue);
31+
float convertedValue = (float) Conversion.ConvertTo<float>(jsonEncoded);
32+
Assert.IsTrue(inputValue == convertedValue);
33+
}
34+
catch (Exception ex)
35+
{ throw ex; }
36+
}
37+
2138
}
2239
}

Parse/Public/Utilities/Conversion.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ internal static object ConvertTo<T>(object value)
5454

5555
if (ReflectionHelpers.IsPrimitive(typeof(T)))
5656
{
57-
return (T) Convert.ChangeType(value, typeof(T));
57+
return (T) Convert.ChangeType(value, typeof(T), System.Globalization.CultureInfo.InvariantCulture);
5858
}
5959

6060
if (ReflectionHelpers.IsConstructedGenericType(typeof(T)))
@@ -65,7 +65,7 @@ internal static object ConvertTo<T>(object value)
6565
Type innerType = ReflectionHelpers.GetGenericTypeArguments(typeof(T))[0];
6666
if (ReflectionHelpers.IsPrimitive(innerType))
6767
{
68-
return (T) Convert.ChangeType(value, innerType);
68+
return (T) Convert.ChangeType(value, innerType, System.Globalization.CultureInfo.InvariantCulture);
6969
}
7070
}
7171
Type listType = GetInterfaceType(value.GetType(), typeof(IList<>));

0 commit comments

Comments
 (0)