zoukankan      html  css  js  c++  java
  • .net扩展方法——其他(科学计数法、ToDictionary 去重、List<Guid>转为List<Guid?>)

    .net扩展方法——其他:

    1. ChangeToDecimal:数字科学计数法处理
    2. ToDictionaryEx:ToDictionary 去重
    3. ToListGuid:List<Guid>转为List<Guid?>

    科学计数法的测试调用:

                string val = "7.8E+07";//7.8*10^7(10的7次方) 科学计数法的表示。例如1.03乘10的8次方,可简写为“1.03E+08”的形式
                var result = val.ChangeToDecimal();//结果:78000000M

    扩展方法:

            /// <summary>
            /// 数字科学计数法处理
            /// </summary>
            /// <param name="strData"></param>
            /// <returns></returns>
            public static decimal ChangeToDecimal(this string strData)
            {
                decimal dData = 0.0M;
                if (strData.Contains("E"))
                {
                    dData = Convert.ToDecimal(decimal.Parse(strData.ToString(), System.Globalization.NumberStyles.Float));
                }
                else
                {
                    dData = Convert.ToDecimal(strData);
                }
                return Math.Round(dData, 4);
            }
    
            /// <summary>
            /// ToDictionary 去重
            /// </summary>
            /// <typeparam name="TElement"></typeparam>
            /// <typeparam name="TKey"></typeparam>
            /// <typeparam name="TValue"></typeparam>
            /// <param name="source"></param>
            /// <param name="keyGetter"></param>
            /// <param name="valueGetter"></param>
            /// <returns></returns>
            public static IDictionary<TKey, TValue> ToDictionaryEx<TElement, TKey, TValue>(
                this IEnumerable<TElement> source,
                Func<TElement, TKey> keyGetter,
                Func<TElement, TValue> valueGetter)
            {
                IDictionary<TKey, TValue> dict = new Dictionary<TKey, TValue>();
                foreach (var e in source)
                {
                    var key = keyGetter(e);
                    if (dict.ContainsKey(key))
                    {
                        continue;
                    }
    
                    dict.Add(key, valueGetter(e));
                }
                return dict;
            }
    
            /// <summary>
            /// List<Guid?>转为List<Guid>
            /// </summary>
            /// <param name="val"></param>
            /// <returns></returns>
            public static List<Guid> ToListGuid(this List<Guid?> val)
            {
                return val.Select(x => x ?? Guid.Empty).ToList();
            }
    
            /// <summary>
            /// List<Guid>转为List<Guid?>
            /// </summary>
            /// <param name="val"></param>
            /// <returns></returns>
            public static List<Guid?> ToListGuid(this List<Guid> val)
            {
                return val.Select(x => new Guid?(x)).ToList();
            }
  • 相关阅读:
    ES基本介绍
    Mybatis 读写分离简单实现
    分享一个Flink checkpoint失败的问题和解决办法
    一次“内存泄露”引发的血案
    记一次堆外内存泄漏排查过程
    MySQL主从复制读写分离,看这篇就够了!
    JVM运行时内存数据区域
    .NET MVC 页面传值方式
    jQuery 对表格内容进行搜索筛选
    泛型
  • 原文地址:https://www.cnblogs.com/amusement1992/p/13496130.html
Copyright © 2011-2022 走看看