private string formatSizeBinary(Int64 size, Int32 decimals = 2)
{
string[] sizes = { "Bytes", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB" };
double formattedSize = size;
Int32 sizeIndex = 0;
while (formattedSize >= 1024 & sizeIndex < sizes.Length)
{
formattedSize /= 1024;
sizeIndex += 1;
}
return string.Format("{0} {1}", Math.Round(formattedSize, decimals).ToString(), sizes[sizeIndex]);
}
我得到了这个
“不允许使用默认参数说明符”
"Int32 decimals = 2"
上的错误
答案 0 :(得分:1)
由于您的代码对我来说很好,但Optional parameters
附带Visual Studio 2010(可能还有.NET 4.0框架)
Visual C#2010引入了命名和可选参数
您需要一个类似的方法
private string formatSizeBinary(Int64 size, Int32 decimals, int value)
{
decimals = value;
string[] sizes = { "Bytes", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB" };
double formattedSize = size;
Int32 sizeIndex = 0;
while (formattedSize >= 1024 & sizeIndex < sizes.Length)
{
formattedSize /= 1024;
sizeIndex += 1;
}
return string.Format("{0} {1}", Math.Round(formattedSize, decimals).ToString(), sizes[sizeIndex]);
}
然后你可以称它为你想要的值;
formatSizeBinary(yoursize, decimals, 2);
formatSizeBinary(yoursize, decimals, 3);
formatSizeBinary(yoursize, decimals, 4);