Ada*_*vis 48
1024对于程序中的使用是正确的.
您可能存在差异的原因可能是由于driveinfo报告为"可用空间"以及Windows认为可用空间的差异.
请注意,只有驱动器制造商使用1,000.在Windows和大多数程序中,正确的缩放是1024.
此外,虽然您的编译器无论如何都应该优化它,但是这个计算可以通过仅为每个幅度移位10来完成:
KB = B >> 10
MB = KB >> 10 = B >> 20
GB = MB >> 10 = KB >> 20 = B >> 30
虽然为了便于阅读,我预计连续除以1024会更清楚.
小智 11
1024实际上是错误的.国际工程界(IEC)在2000年制定了一项标准,令人遗憾地被计算机行业所忽视.这个标准基本上就是说
您可以在IEC SI区域读取它们.
因此,为了使您的转换正确并且符合国际标准化,您应该使用这种科学记数法.
AZ_*_*AZ_ 11
/// <summary>
/// Function to convert the given bytes to either Kilobyte, Megabyte, or Gigabyte
/// </summary>
/// <param name="bytes">Double -> Total bytes to be converted</param>
/// <param name="type">String -> Type of conversion to perform</param>
/// <returns>Int32 -> Converted bytes</returns>
/// <remarks></remarks>
public static double ConvertSize(double bytes, string type)
{
try
{
const int CONVERSION_VALUE = 1024;
//determine what conversion they want
switch (type)
{
case "BY":
//convert to bytes (default)
return bytes;
break;
case "KB":
//convert to kilobytes
return (bytes / CONVERSION_VALUE);
break;
case "MB":
//convert to megabytes
return (bytes / CalculateSquare(CONVERSION_VALUE));
break;
case "GB":
//convert to gigabytes
return (bytes / CalculateCube(CONVERSION_VALUE));
break;
default:
//default
return bytes;
break;
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
return 0;
}
}
/// <summary>
/// Function to calculate the square of the provided number
/// </summary>
/// <param name="number">Int32 -> Number to be squared</param>
/// <returns>Double -> THe provided number squared</returns>
/// <remarks></remarks>
public static double CalculateSquare(Int32 number)
{
return Math.Pow(number, 2);
}
/// <summary>
/// Function to calculate the cube of the provided number
/// </summary>
/// <param name="number">Int32 -> Number to be cubed</param>
/// <returns>Double -> THe provided number cubed</returns>
/// <remarks></remarks>
public static double CalculateCube(Int32 number)
{
return Math.Pow(number, 3);
}
//Sample Useage
String Size = "File is " + ConvertSize(250222,"MB") + " Megabytes in size"
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
53349 次 |
| 最近记录: |