Skip to content

Commit

Permalink
fix: Number Format Exception when Converting decimal from JavaScript 🐛
Browse files Browse the repository at this point in the history
Convert.ChangeType would throw a Number Format Exception when the current Culture was not that of a '.' decimal place.
The fix was to update the Convert.ChangeType to use an InvariantCluture when changing the type of a decimal/float number.

fixes #32
  • Loading branch information
canhorn committed Sep 14, 2021
1 parent 6ec4127 commit 06dc80c
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
@using System.Globalization
<div>
<h3>Literal Decimal Number Cluture Info Validation</h3>
<div class="--lighter">Interop Get</div>
<div>
Status:
@if (TestStatus == "Passed")
{
<span class="green-badge">@TestStatus</span>
}
else if (TestStatus == "Failed")
{
<span class="red-badge">@TestStatus</span>
}
else
{
<span>@TestStatus</span>
}
</div>
<button class="run-btn" @onclick="HandleRunTest">Run</button>
</div>

<script suppress-error="BL9992">
(function () {
window["InteropDecimalNumberClutureInfoTest"] = {
value: 0.0000000000000001334646852585,
};
})();
</script>

@code {
public string TestStatus = "Pending";

private string testId => "InteropDecimalNumberClutureInfoTest";
private decimal result;
private decimal expected = 0.0000000000000001334646852585m;

private void HandleRunTest()
{
var clutureInfo = CultureInfo.CurrentCulture;
try
{
// Using de-DE because the decimal place is ',' for decimal/numbers.
CultureInfo.CurrentCulture = new CultureInfo(
"de-DE"
);

RunTest();
ValidateTest();
}
catch { }
finally
{
// Reset back to ClutureInfo before test changed it.
CultureInfo.CurrentCulture = clutureInfo;
}
}

public void RunTest()
{
result = EventHorizonBlazorInterop.Get<decimal>(
testId,
"value"
);
}

public void ValidateTest()
{
if (result == expected)
{
TestStatus = "Passed";
}
else
{
TestStatus = "Failed";
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<h1>Interop Validations</h1>

<div class="testing-content">
<InteropDecimalNumberClutureInfoTest />
<InteropDecimalNumberTest />
<InteropFloatNumberTest />
<InteropIntNumberTest />
Expand Down
10 changes: 6 additions & 4 deletions EventHorizon.Blazor.Interop/EventHorizonBlazorInterop.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
namespace EventHorizon.Blazor.Interop
{
using System;
using System.Globalization;
using System.Threading.Tasks;
using Microsoft.JSInterop;
using Microsoft.JSInterop.WebAssembly;
Expand Down Expand Up @@ -209,9 +210,9 @@ params object[] args
/// <example>
/// <code>
/// <![CDATA[
/// EventHorizonBlazorInterop.Get<Vector3>(
/// entity => new Vector3(entity),
/// "document.createElement"
/// EventHorizonBlazorInterop.Get<decimal>(
/// "document",
/// "nodeType"
/// );
/// ]]>
/// </code>
Expand Down Expand Up @@ -240,7 +241,8 @@ string prop
}
return (T)Convert.ChangeType(
result,
Nullable.GetUnderlyingType(typeof(T)) ?? typeof(T)
Nullable.GetUnderlyingType(typeof(T)) ?? typeof(T),
CultureInfo.InvariantCulture
);
}

Expand Down

0 comments on commit 06dc80c

Please sign in to comment.