In debug its value is inside single quotes like this '37'. It must be inside double quotes like this "37" as all string values in c#. When the string value in this single quoted format, double.parse(Employee.Age) throws exception. But when it is in its correct format with double quotes it parses the string to double
When using the Double.Parse method, the parameter should be a string that contains a number to convert. If the value is '37'
, it will throw a FormatException because it is a invalid format.
To prevent this exception, before parse the string, you can extract the number from the string or remove the '
character.
So, try to use the following code:
//Age: string type.
Age = "'20'";
var intnumber = System.Text.RegularExpressions.Regex.Match(Age, @"\d+").Value;
var result = Double.Parse(intnumber);
Or, you can use the following code:
Age = "'20'";
double number;
if (!Double.TryParse(Age, out number))
{
var intnumber = System.Text.RegularExpressions.Regex.Match(Age, @"\d+").Value;
number = Double.Parse(intnumber);
}
The result as below:
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.
Best regards,
Dillion