[navy][i]{ This function returns a formatted string with at the appropriate level of bytes, kilobytes, megabytes, gigabytes, etc }[/i][/navy]
[b]function[/b] FormatBytes(ABytes: Int64; AShortRounding: Boolean = True): String;
[b]const[/b]
suffix : [b]array[/b][[purple]0..6[/purple]] [b]of[/b] String = ([teal]'B'[/teal], [teal]'KB'[/teal], [teal]'MB'[/teal], [teal]'GB'[/teal], [teal]'TB'[/teal], [teal]'PB'[/teal], [teal]'EB'[/teal]);
[b]var[/b]
l : Integer;
f : Double;
[b]begin[/b]
l := [purple]0[/purple];
f := ABytes;
[b]while[/b] (l < High(suffix)) [b]and[/b] (f >= [purple]1024[/purple]) [b]do[/b]
[b]begin[/b]
inc(l);
f := f / [purple]1024[/purple];
[b]end[/b];
[b]if[/b] AShortRounding [b]and[/b] (l < High(suffix)) [b]and[/b] (f >= [purple]1000[/purple]) [b]then[/b]
[b]begin[/b] [navy][i]// ensures eg. 1022 MB will show 0.99 GB
[/i][/navy] inc(l);
f := f / [purple]1024[/purple];
[b]end[/b];
Result := Format([teal]'%f %s'[/teal], [f, suffix[l]]);
[b]end[/b];