double类型相加精度问题解决

 double Sum = Add(Convert.ToDouble(values.GetValue(85, i)), Convert.ToDouble(values.GetValue(86, i)));

 

 

 #region 加
        private double Add(double add1, double add2)
        {
            double Sum=0;
            int a1 = 0;
            int a2 = 0;
            double m = 0;
            try
            {
                a1 = add1.ToString().Split(new char[] { '.' })[1].Length;
            }
            catch (Exception ex)
            {
                a1 = 0;
            }
            try
            {
                a2 = add2.ToString().Split(new char[] { '.' })[1].Length;
            }
            catch (Exception ex)
            {
                a2 = 0;
            }
            m= Math.Pow(10,  Math.Max(a1, a2));
            Sum = (add1 * m + add2 * m) / m;
            return Sum;

        }

        #endregion

 

你可能感兴趣的:(double类型相加精度问题解决)