服务端不停的在接收数据,需要把接收到的字节数用一个Lable显示出来
希望显示得规范一点,如显示成下面样式:214 字节 (214 字节)
1.35 KB (1,389 字节)
483 KB (494,598 字节)
1.59 MB (1,671,837 字节)
31.6 MB (33,232,710 字节)
482 MB (505,780,923 字节)
6.21 GB (6,672,463,691 字节)procedure TForm1.ServerSocket1ClientRead(Sender: TObject;
  Socket: TCustomWinSocket);
begin
  //RecByteCount 为全局Int64变量
  RecByteCount := RecByteCount + Socket.ReceiveLength;
  Label1.Caption := IntToStr(RecByteCount);  //把这里按上面所说规范显示
end;

解决方案 »

  1.   

    if RecByteCount > 1024*1024*1024 then  //G
      Label1.Caption := Format('%.2fG%d字节',[RecByteCount/(1024*1024*1024),RecByteCount])
    else if RecByteCount > 1024*1024 then  //M
      Label1.Caption := Format('%.2fM%d字节',[RecByteCount/(1024*1024),RecByteCount])
    else if RecByteCount > 1024 then       //K
      Label1.Caption := Format('%.2fK%d字节',[RecByteCount/(1024),RecByteCount])
    else 
      Label1.Caption := Format('%d字节%d字节',[RecByteCount,RecByteCount])