-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Number Format Exception when Converting decimal from JavaScript 🐛
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
Showing
3 changed files
with
85 additions
and
4 deletions.
There are no files selected for viewing
78 changes: 78 additions & 0 deletions
78
....Sample/Pages/Testing/InteropTesting/Validation/InteropDecimalNumberClutureInfoTest.razor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters