Delphi中检测是否为数字或浮点数字符串的函数

本人水平有限,如有错误,欢迎指正。

在进行文本输入的时候,我们可以通过KeyPress进行输入字符的限制,以达到只能输出数字或浮点数的限制,但有的时候对于一个字符串变量需要进行类型判断,例如:必须是数字字符串或浮点数字符串,就需要自己写函数进行判断了。

function TForm1.isnum(str:string):boolean;//判断是否全为数字(整数)
var
  i:integer;
begin
  for i:=1 to length(str) do
  if not (str[i] in ['0'..'9']) then
  begin
    result:=false;
    exit;
  end;
  result:=true;
end;

function TForm1.IsDigit(S: string): Boolean;//判断是否为浮点数字符串
var
  i, j: integer;
begin
  Result := True;
  j := 0;
  for i := 1 to length(s) do
  begin
    if not (s[i] in ['0'..'9', '.']) then
      Result := False;
    if s[i] = '.' then
      j := j + 1;
  end;
  if j > 1 then
    Result := False;
  if (s[1] = '.') or (s[length(s)] = '.') then
    Result := False;
  s := copy(s, 1, pos('.', S) - 1);
  j := 0;
  for i := 1 to length(s) do
  begin
    if s[I] = '0' then
      j := j + 1;
  end;
  if j > 1 then
    Result := False;
end;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

锋回路转2022

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值