Incorrect Result of Total Download's Code

孤人 提交于 2019-12-25 04:34:34

问题


I use the following code to display the total download and upload. The problem arises when the cumulative download exceeds 2 GB, the result being the number of bits:

var
  Form1: TForm1;
  Downloaded, Uploaded:integer;

implementation

{$R *.dfm}

procedure TForm1.Timer1Timer(Sender: TObject);
begin
  if Downloaded < 1024 then
  Recv.Caption           := FormatFloat(' + Recv: #,0 Bit',Downloaded)
  else if (Downloaded > 1024) and (Downloaded < 1048576) then
  Recv.Caption           := FormatFloat(' + Recv: #,##0.00 Kb',Downloaded/1024)
  else if (Downloaded > 1048576) and (Downloaded < 1073741824) then
  Recv.Caption           := FormatFloat(' + Recv: #,##0.00 Mb',Downloaded/1048576)
  else if (Downloaded > 1073741824) then
  Recv.Caption           := FormatFloat(' + Recv: #,##0.00 Gb', Downloaded/1073741824);

  if Uploaded < 1024 then
  Sent.Caption               := FormatFloat(' + Sent: #,0 Bit',Uploaded)
  else if (Uploaded > 1024) and (Uploaded < 1048576) then
  Sent.Caption               := FormatFloat(' + Sent: #,##0.00 Kb',Uploaded/1024)
  else if (Uploaded > 1048576) and (Uploaded < 1073741824) then
  Sent.Caption               := FormatFloat(' + Sent: #,##0.00 Mb',Uploaded/1048576)
  else if (Uploaded > 1073741824) then
  Sent.Caption               := FormatFloat(' + Sent: #,##0.00 Gb', Uploaded/1073741824);
end;

Can anyone explain why it is returning the incorrect result and more importantly, how to fix it so it returns the correct result ? Thank you so much...


回答1:


An Integer cannot hold a value greater than 2GB (MaxInt is 2147483647, which is ~1.99 GB). If you try to exceed that, it overflows and becomes negative. You need to use Int64 instead.

Also, you should be using B or Bytes instead of Bit. You are not downloading bits, you are downloading bytes.

Try this:

var
  Form1: TForm1;
  Downloaded, Uploaded: Int64;

implementation

{$R *.dfm}

function FormatBytes(ABytes: Int64): string;
begin
  if ABytes < 1024 then
    Result := FormatFloat('#,0 B', ABytes)
  else if ABytes < 1048576 then
    Result := FormatFloat('#,##0.00 Kb', ABytes / 1024)
  else if ABytes < 1073741824 then
    Result := FormatFloat('#,##0.00 Mb', ABytes / 1048576)
  else if ABytes < 1099511627776 then
    Result := FormatFloat('#,##0.00 Gb', ABytes / 1073741824)
  else
    Result := FormatFloat('#,##0.00 Tb', ABytes / 1099511627776);
end;

procedure TForm1.Timer1Timer(Sender: TObject);
begin
  Recv.Caption := ' + Recv: ' + FormatBytes(Downloaded);
  Send.Caption := ' + Sent: ' + FormatBytes(Uploaded);
end;


来源:https://stackoverflow.com/questions/30418940/incorrect-result-of-total-downloads-code

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!