如何正确地转换文件大小以字节为单位成百万或千兆字节? [英] How to correctly convert filesize in bytes into mega or gigabytes?

查看:994
本文介绍了如何正确地转换文件大小以字节为单位成百万或千兆字节?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用我的C#项目的DriveInfo类来检索给定的驱动器可用的字节数。如何正确我这个号码转换成兆或千兆字节? 1024除以不会做我猜的工作。结果总是从Windows的资源管理器中显示的有所不同。

I'm using the DriveInfo class in my C# project to retrieve the available bytes on given drives. How to I correctly convert this number into Mega- or Gigabytes? Dividing by 1024 will not do the job I guess. The results always differ from those shown in the Windows-Explorer.

推荐答案

1024是正确的程序中使用。

1024 is correct for usage in programs.

你可能有差异的原因可能是由于什么driveinfo为可用空间,什么窗户认为可用空间报告。

The reason you may be having differences is likely due to differences in what driveinfo reports as "available space" and what windows considers available space.

请注意,只有驱动器制造商使用1000。在窗口和大多数程序正确的比例为1024。

Note that only drive manufacturers use 1,000. Within windows and most programs the correct scaling is 1024.

此外,当你的编译器应该优化此无论如何,这种计算可以由10个用于各个仅仅把这些位完成大小:

Also, while your compiler should optimize this anyway, this calculation can be done by merely shifting the bits by 10 for each magnitude:

KB = b >> 10

MB = KB >> 10 = b >> 20

GB = MB >> 10 = KB >> 20 = b >> 30

KB = B >> 10
MB = KB >> 10 = B >> 20
GB = MB >> 10 = KB >> 20 = B >> 30

虽然我可读性预计连续除以1024更清晰。

Although for readability I expect successive division by 1024 is clearer.

这篇关于如何正确地转换文件大小以字节为单位成百万或千兆字节?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆