Skip to content

Commit

Permalink
parse float adjustments
Browse files Browse the repository at this point in the history
Signed-off-by: David Hernando <david.hernando@make.services>
  • Loading branch information
davidatwhiletrue committed Aug 23, 2024
1 parent 4b4907a commit ba988f0
Showing 1 changed file with 25 additions and 1 deletion.
26 changes: 25 additions & 1 deletion Docs/Demos/NCTLWebExplorer/Pages/TransferCspr.razor.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Globalization;
using System.Numerics;
using Casper.Network.SDK;
using Casper.Network.SDK.JsonRpc;
Expand Down Expand Up @@ -80,7 +81,7 @@ async Task SendTransferBtnClicked()
if (string.IsNullOrWhiteSpace(_transferAmount))
throw new Exception();

var cspr = float.Parse(_transferAmount);
var cspr = ParseFloat(_transferAmount);
if (cspr < 2.5)
throw new Exception();
var motes = (ulong)(cspr * 1_000_000_000);
Expand Down Expand Up @@ -138,4 +139,27 @@ async Task SendTransferBtnClicked()
_targetPublicKey = null;
_transferAmount = null;
}

public static float ParseFloat(string input)
{
// Create a NumberFormatInfo object to allow both comma and dot as decimal separators
NumberFormatInfo format = new NumberFormatInfo();
format.NumberDecimalSeparator = ",";

// Try to parse with comma as decimal separator
if (float.TryParse(input, NumberStyles.Float, format, out float result))
{
return result;
}

// If that fails, try parsing with dot as decimal separator
format.NumberDecimalSeparator = ".";
if (float.TryParse(input, NumberStyles.Float, format, out result))
{
return result;
}

// Handle the case where parsing fails (optional)
throw new FormatException($"Unable to parse '{input}' as a float.");
}
}

0 comments on commit ba988f0

Please sign in to comment.