public
static
bool
IsNumber(
string
expression)
{
if(expression.Trim().Length == 0)
{
return false;
}
expression = expression.Trim();
bool hasDecimal = false;
int startIdx = 0;
if(expression.StartsWith("-"))
{
startIdx = 1;
}
for(int i = startIdx;i<expression.Length;i++)
{
// Check for decimal
if (expression[i] == '.')
{
if (hasDecimal) // 2nd decimal
return false;
else // 1st decimal
{
// inform loop decimal found and continue
hasDecimal = true;
continue;
}
}
// check if number
if(!char.IsNumber(expression[i]))
return false;
}
return true;
}
{
if(expression.Trim().Length == 0)
{
return false;
}
expression = expression.Trim();
bool hasDecimal = false;
int startIdx = 0;
if(expression.StartsWith("-"))
{
startIdx = 1;
}
for(int i = startIdx;i<expression.Length;i++)
{
// Check for decimal
if (expression[i] == '.')
{
if (hasDecimal) // 2nd decimal
return false;
else // 1st decimal
{
// inform loop decimal found and continue
hasDecimal = true;
continue;
}
}
// check if number
if(!char.IsNumber(expression[i]))
return false;
}
return true;
}
public
static
bool
IsInteger(
string
expression)
{
if(expression.Trim().Length == 0)
{
return false;
}
// leading - is ok
expression = expression.Trim();
int startIdx = 0;
if(expression.StartsWith("-"))
{
startIdx = 1;
}
for(int i=startIdx;i<expression.Length;i++)
{
if(!char.IsNumber(expression[i]))
{
return false;
}
}
return true;
}
{
if(expression.Trim().Length == 0)
{
return false;
}
// leading - is ok
expression = expression.Trim();
int startIdx = 0;
if(expression.StartsWith("-"))
{
startIdx = 1;
}
for(int i=startIdx;i<expression.Length;i++)
{
if(!char.IsNumber(expression[i]))
{
return false;
}
}
return true;
}