怎么判断一个字符串是数字?

CoolDesigner 2003-06-05 11:31:54
怎么判断一个字符串是数字?
...全文
99 20 打赏 收藏 转发到动态 举报
写回复
用AI写文章
20 条回复
切换为时间正序
请发表友善的回复…
发表回复
hiflower 2003-06-05
  • 打赏
  • 举报
回复
v:variant;
s:string;
f:extended;

v:=s;
try
f:=v;
except
//不是数字
end;
gototop99 2003-06-05
  • 打赏
  • 举报
回复
var
I, Code: Integer;
begin
Val(Edit1.Text, I, Code);
if Code <> 0 then
MessageDlg('Error at position: ' + IntToStr(Code), mtWarning, [mbOk], 0)
else
Canvas.TextOut(10, 10, 'Value = ' + IntToStr(I));
end;
foilsman 2003-06-05
  • 打赏
  • 举报
回复
uses SysUtils;


var
s : String;
I : Integer;
begin
s := '2254334';
TryStrToInt(s, i);
if Length(IntToStr(I)) < Length(s) then
ShowMessage('不是一个整数');
end;
darkhorsedba 2003-06-05
  • 打赏
  • 举报
回复
i,j:integer;
val(str,i,j);
if j=0 then
showmessage('str是数字');
xiaoyuer0851 2003-06-05
  • 打赏
  • 举报
回复
呵呵,同意
方法有很多种,关键是 看大家愿意使用哪种
darkhorsedba 2003-06-05
  • 打赏
  • 举报
回复
shao528(远洋)说的对,val比try..except结构要好!!
Comer 2003-06-05
  • 打赏
  • 举报
回复
try
strtofloat('123')
except
end;
shao528 2003-06-05
  • 打赏
  • 举报
回复
val函数可以,具体看help
lion_lh 2003-06-05
  • 打赏
  • 举报
回复
try
strtofloat('fdf')
except
showmessage('错');
end;
yhj78 2003-06-05
  • 打赏
  • 举报
回复
try
strtofloat(变量);/strtoint(变量);
except
showmessage('错误');

end;
这样最简单。
suuare 2003-06-05
  • 打赏
  • 举报
回复
for i:=1 to length(Str) do
begin
if not (Str[i] in [0..9] then)
ShowMessage('不是!');

end;
nuaabestbo 2003-06-05
  • 打赏
  • 举报
回复
val(str,i,j);
if j=0 then showmessage('str是数字');
ilovedbb 2003-06-05
  • 打赏
  • 举报
回复
val
风行者009 2003-06-05
  • 打赏
  • 举报
回复
if not (key int ['0'..'9',#48,#8]) then
begin
key:=#0;
end;
kellerlee 2003-06-05
  • 打赏
  • 举报
回复
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;

type
TForm1 = class(TForm)
Button1: TButton;
Edit1: TEdit;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
function nu(mstr:string):boolean;
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}
function TForm1.nu(mStr: string): Boolean; { 返回字符串是否是正确的数字表达 }

var

I: Real;

E: Integer;

begin

Val(mStr, I, E);

Result := E = 0;

E := Trunc(I);

end;

procedure TForm1.Button1Click(Sender: TObject);


begin

if nu(edit1.Text )=true then
application.MessageBox('全是数字','',mb_ok)
else
application.MessageBox('全是数字','',mb_ok)
end;


end.
wanjichuansuo 2003-06-05
  • 打赏
  • 举报
回复
function isNum(s:string):Boolean;
var
p:pchar;
i,j:integer;
begin
Result:=true;
i:=Strlen(s);
GetMem(p,128);
StrPcopy(p,s);
For j:=0 to i-1 do
begin
if not(p[i] in [0..9]) then
Result:=false;
end;
Freemem(p,128);
end;
CoolDesigner 2003-06-05
  • 打赏
  • 举报
回复
int 是什么关键字?怎么报错!
dichun 2003-06-05
  • 打赏
  • 举报
回复

try
var,
strtoint,
strtoint64,
strtofloat,
except
end;
在ONKEYPRESS
if not (key int ['0'..'9',#48,#8]) then
begin
key:=#0;
end;
cnhgj 2003-06-05
  • 打赏
  • 举报
回复
也可以在输入时在keypress里判断

if not (key int ['0'..'9',#48]) then
begin
key:=#0;
beep;
end;
CoolDesigner 2003-06-05
  • 打赏
  • 举报
回复
如果类似20.5带有小数点的数?怎么办

5,386

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 开发及应用
社区管理员
  • VCL组件开发及应用社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧