这是一个简单的(我认为)。德尔福函数显示Windows的字节数
是否有一个系统内置函数,或者某人创建了一个可以从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,并且只显示3为KB,MB和GB显著数字。然后通过以逗号分隔数千的方式在圆括号中显示确切的字节数。这是一个很好的展示,经过深思熟虑。
有谁知道这样的功能?
编辑:我很惊讶没有这个功能。
感谢您的帮助。我想出了这个,这似乎工作:
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;
这可能是够好,但有什么更好吗?
StrFormatByteSize64看起来似乎是Windows用于第一部分。但是从Delphi调用该函数需要设置一个缓冲区 - 不太好。对于第二部分,floattostrf似乎比链接中的解决方案更好。 – lkessler 2009-08-17 04:31:24
,因为您有一些想法可以输出多长时间,使用char的压缩数组[1..n]作为缓冲区。通过@CharArray [1]作为指针。 – 2009-08-17 08:34:31
优秀的提示! (StrFormatByteSizeW) – 2015-03-24 19:46:28