0
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"

4

1 回答 1

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);
于 2013-03-26T08:42:27.767 回答