如题.现在是从其他地方获得一串字符,但是要判断这串字符是否为数值型(包括浮点)然后自动转换!
请高手们给个例子吧,谢谢啦

解决方案 »

  1.   

    var
      a :Double;
    ...
    try
      a := StrToFloat('1.0345');
    except
      ShowMessage('不是浮点数');
    end;
      

  2.   

    自己写个循环语句也可以搞定:
    for i:=0 to Length(str)-1 do
      if str[i] not in ['0'..'9','.'] then
         ShowMessage('不是浮点数');
    以上代码仅供参考,可能有些问题,因为我现在电脑上没有delphi,所以没做测试。好象String类型变量str的第一位str[0]保存的是长度,我也记不确切了,楼主感兴趣的话可以试试。
      

  3.   

    在Keypress事件里过滤
    if key not in [#48..#57] then
    dey:=#0;
      

  4.   

    keyPress事件中if not (key in ['0'..'9','.',#8]) then key:=#0
      

  5.   

    既然是不确定的类型,就用 Variant 类型:
    var
    a:Variant
      

  6.   

    1楼就是正解
    强制转换,如果不是就从except里报错
      

  7.   

    支持 Kevin_Lmx(繁华阅尽) !
      

  8.   

    回复人: smilefox2000(遥远的绝响) ( ) 信誉:100  2004-12-19 15:47:00  得分: 0  
     
     
       在Keypress事件里过滤
    if key not in [#48..#57] then
    dey:=#0;
     
     
    ==============
    补充一下,考虑用户输 删除、回车,箭头 等键。
      

  9.   

    也可以不用一楼这么麻烦
    直接用  trystrtofloat函数就可以了
    呵呵
      

  10.   

    var
      a :Double;
    ...
    try
      a := StrToFloat(Self.edit1.text);//转换
    except
      MessageBox(....);//Error
    end;
      

  11.   

    var
      a :Double;
    ...
      if TryStrToFloat(edit1.text, a) then
         ...  //是数值型,同时值已转换并保存到变量a
      else
         ... //不是数值型
      

  12.   

    TryStrToFloat
    又学到一招,记住了补充一点:for i:=0 to Length(str)-1 do
      if str[i] not in ['0'..'9','.'] then
         ShowMessage('不是浮点数');这种方法不行,比如,Str = '0.0.0.0.0.',用这种方法判断是一个数值,但实际上不是
      

  13.   

    Result:=TextToFloat(PChar(S), Value, fvExtended);