Delphi中数字输入限制方法

来源:互联网 发布:c4d最新版软件下载 编辑:程序博客网 时间:2024/05/23 13:45
1、控制Edit中的输入内容为金钱
  在Edit的KeyPress事件中输入以下代码:
procedure Form1.Edit1KeyPress(Sender: TObject; var Key: Char);
var
i:Integer;
begin
  if not (Key in [Chr(8), '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', #46]) then
     Key := #0;
  if key = #46 then
  begin
     for i := 1 to Length(Edit1.Text) do
     begin
       if Copy(Edit1.Text, i, 1) = '.' then
       key := #0;
     end;
  end;
end;
2、计算后四舍五入可以使用Roundto函数,但必须引用math单元,如果计算结果是123.457,想要保留小
   数点后两位数,可以使用Roundto(123.457,-2),运行后结果显示123.46
0 0