新建一个C#控制台项目,要新建.NetFramework类型的,不然下面代码中的某些API无法使用。
代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Test1
{
class Program
{
static void Main(string[] args)
{
List resultList = ScaleConversion(new List() { 230, 2453, 4353, 65 }, 10);
string value = string.Empty;
for (int i = 0; i < resultList.Count; i++)
{
value += string.Format("{0},", resultList[i]);
}
Console.WriteLine(value);
Console.ReadKey();
}
///
/// 将数组等比例缩放
///
/// 数组
/// 缩放的最大值,最小值默认为0
/// 缩放后的数组
public static List ScaleConversion(List valueList, int maxScale)
{
if (valueList == null || valueList.Count == 0)
{
Console.WriteLine("valueList 不能为空");
return null;
}
if (maxScale < 10)
{
Console.WriteLine("等比例的最大值不能小于10");
return null;
}
double max = valueList.Max();
double factor = Math.Round(max / maxScale, 2);//系数
List result = new List();
for (int i = 0; i < valueList.Count; i++)
{
result.Add(Math.Round(valueList[i] / factor, 2));
}
if (result.Count > 0)
return result;
return null;
}
}
}
运行后,结果是:0.53,5.64,10,0.15,
这里是将10做为缩放比例中的最大值,有兴趣的读者可以自己算一下是否正确。
end



