program FormatBytesDemo;
uses
SysUtils; // Format
function FormatBytes(Bytes: Int64): string;
const
Sizes: array[0..4] of string = ('B', 'KB', 'MB', 'GB', 'TB');
var
I: Integer;
DblByte: Real;
begin
I := 0;
DblByte := Bytes;
while (I < 5) and (Bytes >= 1024) do
begin
DblByte := Bytes / 1024.0;
Bytes := Bytes div 1024;
Inc(I);
end;
FormatBytes := Format('%.2f %s', [DblByte, Sizes[I]]);
end;
begin
WriteLn(FormatBytes(9823453784599));
WriteLn(FormatBytes(7124362542));
WriteLn(FormatBytes(23746178));
WriteLn(FormatBytes(1048576));
WriteLn(FormatBytes(1024000));
WriteLn(FormatBytes(873445));
WriteLn(FormatBytes(1024));
WriteLn(FormatBytes(978));
WriteLn(FormatBytes(13));
WriteLn(FormatBytes(0));
end.
(*
run:
8.93 TB
6.63 GB
22.65 MB
1.00 MB
1000.00 KB
852.97 KB
1.00 KB
978.00 B
13.00 B
0.00 B
*)