Reputation: 11
So I've looked around here a bit, but all the solutions that seem like they should work are not working for me. I have a string of text being entered into an input field. I want to convert that string to a float so that the user can enter a monetary value like "234.34".
I have already tried the following:
try
{
float number = (float) Convert.ToDouble(_accountAmountInput.text);
}catch(Exception e)
{
Debug.Log(e + "\n must be number");
}
AND
try
{
float number = float.Parse(_accountAmountInput.text);
}catch(Exception e)
{
Debug.Log(e + "\n must be number");
}
AND
if(float.TryParse(_accountAmountInput.text, NumberStyles.Any, CultureInfo.InstalledUICulture, out float number))
{
Debug.Log("Number: " + number);
}
else
{
//display error screen
//account amount needs to be digits only
}
The last one outputs the correct number but I still get an error message I'm always entering a valid float value.
The error message I'm always getting is :
FormatException: Input string was not in a correct format. System.Number.StringToNumber (System.String str, System.Globalization.NumberStyles options, System.Number+NumberBuffer& number, System.Globalization.NumberFormatInfo info, System.Boolean parseDecimal) (at <1f0c1ef1ad524c38bbc5536809c46b48>:0)
Upvotes: 1
Views: 312