这是一个简单的问题(我认为)。
是否有一个系统内置函数,或者某人创建的可以从 Delphi 调用的函数,它将显示多个字节(例如文件大小),就像 Windows 在文件属性框中显示的方式一样?
例如这是 Windows 属性框显示各种尺寸的方式:
539 bytes (539 bytes)
35.1 KB (35,974 bytes)
317 MB (332,531,365 bytes)
2.07 GB (2,224,617,077 bytes)
显示屏会智能地使用字节、KB、MB 或 GB,并且仅显示 KB、MB 和 GB 的 3 位有效数字。然后,在括号中显示确切的字节数,并用逗号分隔千位。这是一个非常漂亮的展示,经过深思熟虑。
有谁知道这样的功能吗?
编辑:我很惊讶没有这个功能。
感谢您提供有用的想法。我想出了这个,这似乎有效:
function BytesToDisplay(A:int64): string;
var
A1, A2, A3: double;
begin
A1 := A / 1024;
A2 := A1 / 1024;
A3 := A2 / 1024;
if A1 < 1 then Result := floattostrf(A, ffNumber, 15, 0) + ' bytes'
else if A1 < 10 then Result := floattostrf(A1, ffNumber, 15, 2) + ' KB'
else if A1 < 100 then Result := floattostrf(A1, ffNumber, 15, 1) + ' KB'
else if A2 < 1 then Result := floattostrf(A1, ffNumber, 15, 0) + ' KB'
else if A2 < 10 then Result := floattostrf(A2, ffNumber, 15, 2) + ' MB'
else if A2 < 100 then Result := floattostrf(A2, ffNumber, 15, 1) + ' MB'
else if A3 < 1 then Result := floattostrf(A2, ffNumber, 15, 0) + ' MB'
else if A3 < 10 then Result := floattostrf(A3, ffNumber, 15, 2) + ' GB'
else if A3 < 100 then Result := floattostrf(A3, ffNumber, 15, 1) + ' GB'
else Result := floattostrf(A3, ffNumber, 15, 0) + ' GB';
Result := Result + ' (' + floattostrf(A, ffNumber, 15, 0) + ' bytes)';
end;
这可能已经足够好了,但是还有更好的吗?