Delphi函数像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,并且仅显示 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;
这可能已经足够好了,但是还有更好的吗?
This is a simple one (I think).
Is there a system built in function, or a function that someone has created that can be called from Delphi, that will display a number of bytes (e.g. a filesize), the way Windows displays in a file's Properties box?
e.g. This is how Windows property box displays various sizes:
539 bytes (539 bytes)
35.1 KB (35,974 bytes)
317 MB (332,531,365 bytes)
2.07 GB (2,224,617,077 bytes)
The display is smart about using bytes, KB, MB or GB, and shows only 3 significant digits for the KB, MB and GB. It then follows that by displaying the exact number of bytes in parenthesis with commas separating the thousands. It is a very nice display, well thought out.
Does anyone know of such a function?
Edit: I'm very surprised there wasn't a function for this.
Thanks for your helpful ideas. I've come up with this, which seems to work:
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;
This is probably good enough, but is there anything better?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
请参阅以下函数,全部位于 shlwapi 库< /a>.
StrFormatByteSizeA
(DWord 参数)StrFormatByteSizeW
(Int64 参数)StrFormatByteSize64
(在 Unicode 模式下,它实际上是StrFormatByteSizeW
)StrFormatByteSizeEx
(Vista SP2;可以控制舍入)它们中的任何一个都会为您提供所需显示格式的第一部分。检查文档或编写您自己的测试,以确认它们提供了您期望的关于 1 MB 是由 1000 还是 1024 KB 组成的转换。对于显示格式的第二部分,您可以从另一个 Stack Overflow 问题的答案开始:
但也许这个问题是错误的,因为那里的所有建议以及
FloatToStrF
,在Int64
上限处失败。您将丢失一些字节,但我认为这些字节非常重要,因为该显示格式中第二个值的目的是提供准确的数字。完成所有部件后,将它们粘在一起。我在这里使用一个假设的
IntToStrCommas
函数,如果您想将其实现为FloatToStrF
,请继续。See the following functions, all in the shlwapi library.
StrFormatByteSizeA
(DWord parameter)StrFormatByteSizeW
(Int64 parameter)StrFormatByteSize64
(In Unicode mode, it's reallyStrFormatByteSizeW
)StrFormatByteSizeEx
(Vista SP2; can control rounding)Any of them will give you the first portion of your desired display format. Check the documentation or write your own tests to confirm that they give the conversions you expect regarding whether a megabyte consists of 1000 or 1024 kilobytes. For the second part of your display format, you can start with the answers to another Stack Overflow question:
But perhaps that question is the wrong avenue to go down since all the suggestions there, as well as
FloatToStrF
, fail at the upper limits ofInt64
. You'll lose a few bytes, but I consider those pretty important bytes since the purpose of the second value in that display format is to provide an exact number.Once you have all the pieces, glue them together. I'm using a hypothetical
IntToStrCommas
function here, and if you want to implement that asFloatToStrF
, go ahead.不完全是你想要的,但我的库中有一个函数可以让你知道如何解决这个问题:
Not exactly what you're after but I have a function in my library that may give you an idea how to tackle this one:
这是来自我的 dzlib 单元 u_dzConvertUtils :
This is from my dzlib unit u_dzConvertUtils: