数字有很多种格式,不同国家使用的数字格式不同,比如:
123.456.789,00(小数用逗号分隔)
123'456'789.00(千位分隔用')
123,456,789.00(我们常用的)
好像还有别的格式。
当用StrtoFloat时,这些可能就会报错
比如在中文的格式条件下,用strtofloat(23.456.789,00)肯定会提示不是数字。有没有比较好的处理方式,能够兼容所有的数字格式,在使用strtofloat时不报错

解决方案 »

  1.   


    procedure TForm1.Button1Click(Sender: TObject);
    var
    i:Double;
    begin
      {如果可以将EDIT1的值传换成数字型就将转换后的值赋给I并返回TRUE,否则就返回假}
      if TryStrToFloat(edit1.Text,i) then begin
       {----你的操作----}
      end
      else
      showmessage('输入的不是正确的数字');
    end;
      

  2.   

    但dephpi不会认为
    例如:123.456.789,00这样的格式是数字,所以要转的时候你要把.去掉把,换成.才行
      

  3.   

    电脑只认:123456789.00或123456789格式的
    对于你的问题这样吧:procedure TForm1.BitBtn1Click(Sender: TObject);
    var
    str,strb,strc,strd:string;
    i:Double;
    begin
      str:=edit1.Text;
      strb:=StringReplace(copy(str,length(str)-2,maxint),',','.',[rfReplaceAll]);
      strc:=StringReplace(copy(str,1,length(str)-3),'.','',[rfReplaceAll]);
      strc:=StringReplace(strc,'''','',[rfReplaceAll]);
      strc:=StringReplace(strc,',','',[rfReplaceAll]);
      strd:=strc+strb;
      //edit2.Text:=strd; {如果可以将EDIT1的值传换成数字型就将转换后的值赋给I并返回TRUE,否则就返回假}
      if TryStrToFloat(strd,i) then begin
       edit2.Text:=floattostr(i);
      end
      else
      showmessage('输入的不是正确的数字');end;
      

  4.   

    为怕你误会修正一下:procedure TForm1.BitBtn1Click(Sender: TObject);
    var
    str,strb,strc,strd:string;
    i:Double;
    begin
      str:=edit1.Text;
      strb:=StringReplace(copy(str,length(str)-2,maxint),',','.',[rfReplaceAll]);
      strc:=StringReplace(copy(str,1,length(str)-3),'.','',[rfReplaceAll]);
      strc:=StringReplace(strc,'''','',[rfReplaceAll]);
      strc:=StringReplace(strc,',','',[rfReplaceAll]);
      strd:=strc+strb;
     {如果可以将EDIT1的值传换成数字型就将转换后的值赋给I并返回TRUE,否则就返回假}
      if TryStrToFloat(strd,i) then begin
       edit2.Text:=FormatFloat('0.00',(i));
      end
      else
      showmessage('输入的不是正确的数字');end;
      

  5.   

    注:窗体中要用两个edit控件,其中EDIT1用作输入的,EDIT2用作显示转换后的结果