# Simple solution in C#

• The main problem in validating the number is exponential and rules related to that. If we break the given string into two parts, it become easy to validate the two parts individually because each part has it's own set of rules.

``````        /// <summary>
/// Validate the numbers having exponential values as well
/// </summary>
/// <param name="s">string to validate as number</param>
/// <returns>true if valid</returns>
public bool IsNumber(string s)
{
// remove extra spaces from both the ends.
s = s.Trim();

// check if the length is 0
if (s.Length == 0)
return false;

// has space inbetween
if (s.IndexOf(' ') > -1)
{
return false;
}

// only decimal
if (s.IndexOf('.') > -1 && s.Length == 1)
return false;

// check number of decimals
string[] str = s.Split('.');
// has more than one decimal point.
if (str.Count() > 2)
{
return false;
}

// if the number has exponetials
if (s.IndexOf('e') > -1)
{
// break the number into two parts on the basis of 'e'
string[] expo = s.Split('e');

// more then 1 'e' exists or
if (expo.Length > 2)
{
return false;
}

// validate left
string left = expo[0];

// 'e' is first character.
if (left.Length == 0)
return false;

if (left.Length == 1 && (left[0] < '0' && left[0] > '9'))
return false;

for (int i = 0; i < left.Length; i++)
{
if (i == 0 && (left[i] == '+' || left[i] == '-') && left.Length > 1)
continue;
else if (left[i] == '.' && left.Length > 1)
continue;
else if (left[i] >= '0' && left[i] <= '9')
continue;
else
return false;
}

// validate right
string right = expo[1];

// 'e' is last character.
if (right.Length == 0)
return false;

if (right.Length == 1 && (right[0] < '0' && right[0] > '9'))
return false;

for (int i = 0; i < right.Length; i++)
{
if (i == 0 && (right[i] == '+' || right[i] == '-') && right.Length > 1)
continue;
else if (right[i] >= '0' && right[i] <= '9')
continue;
else
return false;
}

}
else
{
// remove the decimal if it is in the end of number.
if (s.IndexOf('.') == s.Length - 1)
{
s = s.Substring(0, s.Length - 1);
}

for (int i = 0; i < s.Length; i++)
{
if (i == 0 && (s[i] == '+' || s[i] == '-') && s.Length > 1)
continue;
else if (s[i] >= '0' && s[i] <= '9')
continue;
else if (s[i] == '.' && i != s.Length - 1)
continue;
else
return false;
}
}

return true;
}
``````

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.