经常用到把字节数显示成kb或gb,函数如下
function FormatByteSize(const bytes: Longint): string;
const
B = 1; //byte
KB = 1024 * B; //kilobyte
MB = 1024 * KB; //megabyte
GB = 1024 * MB; //gigabyte
begin
if bytes > GB then
result := FormatFloat('#.## GB', bytes / GB)
else
if bytes > MB then
result := FormatFloat('#.## MB', bytes / MB)
else
if bytes > KB then
result := FormatFloat('#.## KB', bytes / KB)
else
result := FormatFloat('#.## bytes', bytes) ;
end;
本文介绍了一个实用的函数,用于将字节数转换为更易读的形式,如KB、MB或GB。该函数通过判断字节数的大小,将其转换为合适的单位,并保留两位小数。
541

被折叠的 条评论
为什么被折叠?



