我想写一个函数 
判断值是否属于数字型,传入值为字符型
假如不是数字则直接在页面打印出数值错误
如果是数字则转换为数字型输出
各位大大 这样该怎么写呢?

解决方案 »

  1.   

    asp中我是这样写的
    Function chknum(str)
    if IsNumeric(str)=false then
    response.write "数据错误,传入参数必须是数字"
    response.End
    else
    chknum=int(str)
    end if
    End Function
      

  2.   


    using System;namespace CSDN
    {
        class App
        {
            static bool CheckCharIsNumber(char c, out int i)//-99999999作为失败标记
            {
                i = -99999999;
                bool b = char.IsNumber(c);
                if (b)
                {
                    i = Convert.ToInt32(c);
                }
                return b;
            }
            static void Main(string[] args)
            {        }
        }
    }
      

  3.   

     public static bool isdecimal(string txt)  //判断文本框中是是否是数字型
           {
               if (txt == "")
               {
                   return true;
               }
               else
               {
                   try
                   {
                       decimal val = Decimal.Parse(txt);
                       return true;
                   }
                   catch
                   {
                       return false;
                   }
               }
               
           }
      

  4.   

    你可以这样试下....
    public static bool fun(string txt)
          { 
             string str="123456789";
              for(int i=0;i<txt.length;i++)
              {//通过循环判断下所输入的数据每一项是不是一个数字
                  if(str.Contains(txt[i])) 
                  {  } 
                  else 
                 { 
                   return false;//有一个不是则返回false;
                 }   
              }     
              return true;   
          }
      

  5.   

    private double IsNumber(string str)
    {
        double dNum;
        if(double.TryParse(str, out dNum))//根据需要将double替换为其他数值类型
        {
            Console.WriteLine("输入正确!");
        }
        else
        {
            Console.WriteLine("输入的不是数字!");
            dNum = null;
        }    return dNum;
    }
      

  6.   


    private double IsNumber(string str) 

        double dNum; 
        if(double.TryParse(str, out dNum))//根据需要将double替换为其他数值类型 
        { 
            Console.WriteLine("输入正确!"); 
        } 
        else 
        { 
            Console.WriteLine("输入的不是数字!"); 
            dNum = null; 
        }     return dNum; 
    }
    借楼上的。
    欢迎加入我的程序设计QQ群80532706哟
      

  7.   

    大家理解错我的意思了```估计C#是实现不了这样的功能了!
    我的意思是
    int userid=chknum(Request.QueryString["userid"])如果他传入的是数值则会正常运行 反之则在页面打印参数错误并停止程序运行!chknum这个函数不知道要怎么写```