Example #1
0
 public static string SumPrice <T>(this IEnumerable <T> collection,
                                   Func <T, string> selector)
 {
     return(collection.Aggregate("" /*start with an empty MyClass*/,
                                 (a, b) => PriceUtil.Add(a, selector(b))));
 }
Example #2
0
        object AddValue(ColumnDataType datatype,
                        object o1,
                        object o2)
        {
            if (o1 == null && o2 == null)
            {
                return(null);
            }
            if (o1 == null)
            {
                return(o2);
            }
            if (o2 == null)
            {
                return(o1);
            }
            if (datatype == ColumnDataType.Auto)
            {
                if (o1 is Int64)
                {
                    return((Int64)o1 + Convert.ToInt64(o2)); // 2016/11/24
                }
                if (o1 is Int32)
                {
                    return((Int32)o1 + (Int32)o2);
                }
                if (o1 is double)
                {
#if NO
                    if (o2 is long)
                    {
                        return((double)o1 + Convert.ToDouble(o2));
                    }
                    return((double)o1 + (double)o2);
#endif
                    return((double)o1 + Convert.ToDouble(o2));
                }
                if (o1 is decimal)
                {
                    return((decimal)o1 + (decimal)o2);
                }
                if (o1 is string)
                {
                    return((string)o1 + (string)o2);
                }

                throw new Exception("无法支持的 Auto 类型累加 o1 type=" + o1.GetType().ToString() + ", o2 type=" + o2.GetType().ToString());
            }
            if (datatype == ColumnDataType.Number)
            {
                if (o1 is Int64)
                {
                    return((Int64)o1 + (Int64)o2);
                }
                if (o1 is Int32)
                {
                    return((Int32)o1 + (Int32)o2);
                }
                if (o1 is double)
                {
                    return((double)o1 + (double)o2);
                }
                if (o1 is decimal)
                {
                    return((decimal)o1 + (decimal)o2);
                }
                if (o1 is string)   // 2015/7/16
                {
                    Int64 v1 = 0;
                    Int64 v2 = 0;
                    Int64.TryParse(o1 as string, out v1);
                    Int64.TryParse(o2 as string, out v2);
                    return((v1 + v2).ToString());
                }

                throw new Exception("无法支持的 Number 类型累加 o1 type=" + o1.GetType().ToString() + ", o2 type=" + o2.GetType().ToString());
            }
            if (datatype == ColumnDataType.String)
            {
                if (o1 is string)
                {
                    return((string)o1 + (string)o2);
                }

                throw new Exception("无法支持的 String 类型累加 o1 type=" + o1.GetType().ToString() + ", o2 type=" + o2.GetType().ToString());
            }
            if (datatype == ColumnDataType.Price) // 100倍金额整数
            {
                return((Int64)o1 + (Int64)o2);
            }
            if (datatype == ColumnDataType.PriceDouble)  // double,用来表示金额。也就是最多只有两位小数部分 -- 注意,有累计误差问题,以后建议废止
            {
                return((double)o1 + (double)o2);
            }
            if (datatype == ColumnDataType.PriceDecimal) // decimal,用来表示金额。
            {
                return((decimal)o1 + (decimal)o2);
            }
            if (datatype == ColumnDataType.Currency)
            {
#if NO
                // 这一句容易发现列 数据类型 的错误
                return(PriceUtil.JoinPriceString((string)o1,
                                                 (string)o2));
#endif
                return(PriceUtil.Add((string)o1,
                                     (string)o2));

#if NO
                // 这一句更健壮一些
                return(PriceUtil.JoinPriceString(Convert.ToString(o1),
                                                 Convert.ToString(o2)));
#endif
            }
            throw new Exception("无法支持的 " + datatype.ToString() + " 类型累加 o1 type=" + o1.GetType().ToString() + ", o2 type=" + o2.GetType().ToString());
        }