Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Semrush - keyword research tool

Turn ChatGPT, Claude, Gemini, And CoPilot Into Your Personal Assistant, Business Coach, Content Creator, And More

AFFILIATE MARKETING Your all-in-one performance engine Manage affiliates, creators, and customer referrals in one unified platform—turning every partnership into measurable growth
Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

42,656 questions

55,403 answers

573 users

How to format bytes to kilobytes, megabytes, gigabytes and terabytes in Pascal

1 Answer

0 votes
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

*)

 



answered Nov 18, 2025 by avibootz
edited Nov 18, 2025 by avibootz
...