我有一个方法,它接受一个引用双,并返回一个字符串,同时修改引用的值:
const long OneKb = 1024;
const long OneMb = OneKb * 1024;
const long OneGb = OneMb * 1024;
const long OneTb = OneGb * 1024;
public string GetLargestDataSizeAndUnitOfMeasurement(ref double value, int decimalPlaces = 0)
{
var asTb = Math.Round(value / OneTb, decimalPlaces);
var asGb = Math.Round(value / OneGb, decimalPlaces);
var asMb = Math.Round(value / OneMb, decimalPlaces);
var asKb = Math.Round(value / OneKb, decimalPlaces);
string unit = asTb > 1 ? string.Format("Tb", value = asTb)
: asGb > 1 ? string.Format("Gb", value = asGb)
: asMb > 1 ? string.Format("Mb", value = asMb)
: asKb > 1 ? string.Format("Kb", value = asKb)
: string.Format("B", value = Math.Round(value, decimalPlaces));
return unit;
}我的问题是,在中为引用分配一个新值string.Format()是否可以接受,尽管该值与该方法本身无关。如果我想避免这种情况,我可以单独执行if来修改value,但是这看起来更干净,而且在规模上可能更高效。
发布于 2016-04-19 12:54:35
实际上,并不认为在string.format方法中执行赋值调用有任何错误。但是,通过查看给定的代码,可以避免这种情况。
如果您寻找更干净的代码,您可以使用enum来存储数据单元名称,并且可以减少声明的变量的数量。这样,您的代码就是,更易于维护的,如果您想使用更高的数据大小单元,您只需将其添加到枚举列表中即可。
public class DataSizeFormatter
{
const int OneKB = 1024;
private enum DataSizes
{
B,
KB,
MB,
GB,
TB
}
public string GetLargestDataSizeAndUnitOfMeasurement(ref double value, int decimalPlaces = 0)
{
var highestExponent = (int)(Math.Log(value, OneKB)); // Get the highest power which you could assign to 1024 that would not be greater than the given value.
var lengthOfDataSizeEnum = Enum.GetNames(typeof(DataSizes)).Length; //Get the length of the enum list
int highestExponentWithLimit = highestExponent < lengthOfDataSizeEnum ? highestExponent : lengthOfDataSizeEnum - 1; //If the given value could be divided by a higher data unit than in your enum list then only use your highest data size unit.
value = Math.Round(value / Math.Pow(OneKB, highestExponentWithLimit), decimalPlaces); //round of your given value to the approriate data size.
return ((DataSizes)highestExponentWithLimit).ToString(); //return the data size that was used to round of your given value.
}
}更新
看看这些类似的问题,这就解释了为什么在参数内进行分配是很好的:方法参数内的变量分配和为什么赋值语句返回值?。
更新2:
关于将字节转换为更高数据存储单元的最佳方法的问题已经在这里得到了回答:.NET提供了一种将字节转换为KB、MB、GB等的简单方法吗?。
发布于 2016-04-19 18:35:51
在进行赋值时,value = asTb在String.Format()中是有效的。就可读性和使用String.Format()的方式而言,这是意想不到的行为。
MSDN文档说明了使用String.Format()的原因如下:
如果需要将对象、变量或表达式的值插入另一个字符串,请使用String.Format。
基于您的代码片段,这不是您的意图。您只想返回一个格式化的文件大小及其相应的符号。
public class FileSizeConverter
{
public enum FileSizeSymbol
{
B,
KB,
MB,
GB,
TB
}
public string FormatByteSize(ref double fileSize, int decimalPlaces = 0)
{
var unit = FileSizeSymbol.B;
while (fileSize >= 1024 && unit < FileSizeSymbol.TB)
{
fileSize = fileSize / 1024;
unit++;
}
fileSize = Math.Round(fileSize, decimalPlaces, MidpointRounding.AwayFromZero);
return unit.ToString();
}
}发布于 2016-04-19 09:49:51
创建一个具有值和Unit属性的小类、一个ToString方法和一个以double作为参数的构造函数。也许叫它DimensionedNumber。
现在,您只需通过向构造函数传递值来创建DimensionedNumber。如果要显示DimensionedNumber,只需使用ToString()方法即可。
有点像
public class DimensionedNumber
{
public double Value{get; private set;}
public string Dimension {get; private set;}
const double OneKb = 1024.0;
const double OneMb = OneKb * OneKb;
const double OneGb = OneMb * OneKb;
const double OneTb = OneGb * OneKb;
public DimensionedNumber(double value)
{
if (value > OneTb) {
Value = value / OneTb;
Dimension = "Tb";
} else if (value > OneGb) {
Value = value / OneGb;
Dimension = "Gb";
} else if (value > OneMb) {
Value = value / OneMb;
Dimension = "Mb";
} else if (value > OneKb) {
Value = value / OneKb;
Dimension = "Kb";
} else {
Value = value;
Dimension = "";
}
}
public string ToString(int decimalPlaces)
{
return Value.ToString("N" + decimalPlaces.ToString()) + Dimension;
}
}你会用它
var displayValue = new DimenesionedNumber(12345678.9);
Console.WriteLine(displayValue.ToString(3)); // Three decimal placeshttps://stackoverflow.com/questions/36714626
复制相似问题