1. Home
  2. Computing & Technology
  3. Delphi Programming
photo of Zarko Gajic
Zarko's Delphi Programming Blog

By Zarko Gajic, About.com Guide to Delphi Programming since 1998

Formatting a File Size in Bytes for Display - Convert Bytes to Delphi String (KB, MB, GB)

Monday October 13, 2008
in Delphi TIPS :: When working with files from Delphi you might want to display the size of a file to the user in a Explorer-like format where the file size is not displayed in bytes - but the display depends on the size of the actual file.

Read the full article to learn how to Format a File Size in Bytes for Display

Related:

Comments
October 15, 2008 at 1:45 am
(1) Sven Söhnel says:

Hi @all!

First off all: sorry for my bad english.

I use an Unit “xProcs.pas” from Fabula.com since some years.

In this unit is an function i’ve extended to do such a format.
The “extended” version has the option to give the unit (KB/MB/GB/)
or use the next smaller one (hope you could understand):

[code]
{ xTool - Component Collection }
{ Copyright (c) 1995 Stefan Böther }

...

const
{ common computer sizes }
KBYTE = Sizeof(Byte) shl 10;
MBYTE = KBYTE shl 10;
GBYTE = MBYTE shl 10;

...

//The original one:
//function strByteSize(Value: Longint): String;
//
// function FltToStr(F: Extended): String;
// begin
// Result:=FloatToStrF(Round(F),ffNumber,6,0);
// end;
//
//begin
// if Value > GBYTE then
// Result:=FltTostr(Value / GBYTE)+' GB'
// else if Value > MBYTE then
// Result:=FltToStr(Value / MBYTE)+' MB'
// else if Value > KBYTE then
// Result:=FltTostr(Value / KBYTE)+' KB'
// else
// Result:=FltTostr(Value) +' Byte'; { 04.08.96 sb }
//end;

//strByteSize by Söhnel-Software'99

//adapted nr.1
function strByteSize2(bytes: Int64): string;
begin
//From HyperVirtualExplorer_Simple by Bill Miller
result := EmptyStr;
if (0 = 1024) and (bytes = 1048576) and (bytes = 1073741824) then
result := Format('%f GB', [bytes / 1073741824]);
end;

//the most used...
function strByteSize(Value: Real; Mode: string): string;

function FltToStr(F: Extended): string;
begin
Result := FloatToStrF(Round(F), ffNumber, 18, 0);
end;

begin
if Mode = '0' then
Result := FltTostr(Value);
if Mode = '' then
begin
if Value > GBYTE then
Result := FltTostr(Value / GBYTE) + ' GB'
else
if Value > MBYTE then
Result := FltToStr(Value / MBYTE) + ' MB'
else
if Value > KBYTE then
Result := FltTostr(Value / KBYTE) + ' KB'
else
Result := FltTostr(Value) + ' Byte'; { 04.08.96 sb }
exit;
end;
if Mode = '-1' then
begin
if Value > GBYTE then
Result := FltToStr(Value / MBYTE) + ' MB'
else
if Value > MBYTE then
Result := FltTostr(Value / KBYTE) + ' KB'
else
Result := FltTostr(Value) + ' Byte'; { 04.08.96 sb }
exit;
end;
if Mode = 'GB' then
begin
Result := FltTostr(Value / GBYTE) + ' GB';
end;
if Mode = 'MB' then
begin
Result := FltTostr(Value / MBYTE) + ' MB';
end;
if Mode = 'KB' then
begin
Result := FltTostr(Value / KBYTE) + ' KB';
end;
if Mode = 'B' then
begin
Result := FltTostr(Value) + ' Byte';
end;
end;
//strByteSize by Söhnel-Software'99

...

[/code]

January 18, 2009 at 11:21 am
(2) mike says:

j4RQeW hi! how you doin?

February 6, 2009 at 4:49 pm
(3) hoolio says:

A0bxBU hi! this is

August 10, 2009 at 2:56 pm
(4) Amoumbembaria says:

http://wjmenrtedfweprltghuortymuijkujplooo.com wjmenrtedfweprltghuortymuijkujplooo
wjmenrtedfweprltghuortymuijkujplooo
wjmenrtedfweprltghuortymuijkujplooo

August 20, 2009 at 4:34 pm
(5) NesaunseKef says:

http://vbnsnngjwrptylytumukjikhyuj.com vbnsnngjwrptylytumukjikhyuj
vbnsnngjwrptylytumukjikhyuj
vbnsnngjwrptylytumukjikhyuj

November 13, 2009 at 8:33 pm
(6) BupeNeermeD says:

http://vbnnwerfjgjqwepppiloloo.com vbnnwerfjgjqwepppiloloo
vbnnwerfjgjqwepppiloloo
vbnnwerfjgjqwepppiloloo

Leave a Comment

Line and paragraph breaks are automatic. Some HTML allowed: <a href="" title="">, <b>, <i>, <strike>

Explore Delphi Programming
About.com Special Features

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

Easy ways to connect two computers for networking purposes. More >

  1. Home
  2. Computing & Technology
  3. Delphi Programming

©2009 About.com, a part of The New York Times Company.

All rights reserved.