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:


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]
j4RQeW hi! how you doin?
A0bxBU hi! this is
http://wjmenrtedfweprltghuortymuijkujplooo.com wjmenrtedfweprltghuortymuijkujplooo
wjmenrtedfweprltghuortymuijkujplooo
wjmenrtedfweprltghuortymuijkujplooo
http://vbnsnngjwrptylytumukjikhyuj.com vbnsnngjwrptylytumukjikhyuj
vbnsnngjwrptylytumukjikhyuj
vbnsnngjwrptylytumukjikhyuj
http://vbnnwerfjgjqwepppiloloo.com vbnnwerfjgjqwepppiloloo
vbnnwerfjgjqwepppiloloo
vbnnwerfjgjqwepppiloloo