protected int nGoodInt (string sNumber)
{
sNumber = sNumber.Trim();
int Num;
bool isNum = int.TryParse(sNumber, out Num);
if (isNum)
{
return Num;
}
else
{
return 0;
}
}
protected System.Boolean IsNumeric(System.Object Expression)
{
if (Expression == null || Expression is DateTime)
{
return false;
}
if (Expression is Int16 || Expression is Int32 || Expression is Int64 || Expression is Decimal || Expression is Single || Expression is Double || Expression is Boolean)
{
return true;
}
try
{
if (Expression is string)
{
Double.Parse(Expression as string);
return false;
}
else
{
Double.Parse(Expression.ToString());
return true;
}
}
catch
{
return false;
}
}
Function GoodInt(s) ' Returns a "good" number, accounting for null values, etc. On Error Resume Next If len(s) > 0 and isNumeric(s) then s = csng(s) GoodInt= CLng(s) Else GoodInt= 0 End If If Err Then GoodInt= 0 End Function
| Function | String | Value |
|---|---|---|
| GoodInt | 55 | 55 |
| GoodInt | 55.55 | 56 |
| GoodInt | 55.45 | 55 |
| GoodInt | SomeText | 0 |
Originally Posted on April 11, 2013
Last Updated on October 24, 2021
Last Updated on October 24, 2021
All information on this site is shared with the intention to help. Before any source code or program is ran on a production (non-development) system it is suggested you test it and fully understand what it is doing not just what it appears it is doing. I accept no responsibility for any damage you may do with this code.