C#でIsNumericの代用

dobon.net


doubleに変換できるかで判断

string str = "-12.34";

//doubleに変換できるか確かめる
double d;
if (double.TryParse(str, out d))
{
    //変換出来たら、dにその数値が入る
    Console.WriteLine("{0} は数値 {1} に変換できます。", str, d);
}
else
{
    Console.WriteLine("{0} は数字ではありません。", str);
}

//.NET Framework 1.1以前では、以下のようにする
//if (double.TryParse(str,
//    System.Globalization.NumberStyles.Float |
//        System.Globalization.NumberStyles.AllowThousands,
//    System.Globalization.NumberFormatInfo.CurrentInfo,
//    out d))