我正在使用以下函数将字节格式化为更易读的格式,但它返回的信息不正确。
参考资料: http://delphi.about.com/od/delphitips2008/qt/format-bytes.htm(作者:Zarco Gajic)
有人能解释为什么它返回不正确的信息,更重要的是知道如何修复它以返回正确的信息吗?
//Format file byte size
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;
示例:
procedure TForm1.Button1Click(Sender: TObject);
begin
ShowMessage(FormatByteSize(323889675684)); //Returns 1.65GB when it should be ~301GB
end;
参考资料: http://delphi.about.com/od/delphitips2008/qt/format-bytes.htm(作者:Zarco Gajic)
有人能解释为什么它返回不正确的信息,更重要的是知道如何修复它以返回正确的信息吗?
bytes
就可以非常清楚地看到这一点!它显示为 1767128484,而不是 323889675684。 - Marjan VenemaStrFormatByteSizeEx
函数来处理这种情况。 - Victoria