栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > .Net

C# 数组等比例缩放

.Net 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C# 数组等比例缩放

新建一个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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/909678.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号