Пример #1
0
        // 减法
        public static string Subtract(string num1, string num2)
        {
            num1 = Science.MySciToNum(num1);
            num2 = Science.MySciToNum(num2);

            Int32 right1 = Int32.Parse(num1.Substring(num1.IndexOf('E') + 1));
            Int32 right2 = Int32.Parse(num2.Substring(num2.IndexOf('E') + 1));

            decimal left1 = Convert.ToDecimal(num1.Substring(0, num1.IndexOf('E')));
            decimal left2 = Convert.ToDecimal(num2.Substring(0, num2.IndexOf('E')));

            int len = right1 - right2;

            if (right1 >= right2)
            {
                StringBuilder sb  = new StringBuilder();
                string        str = "0." + sb.Append('0', right1 - right2) + left2.ToString().Replace(".", "");
                Console.WriteLine(str);
                decimal  result = left1 - decimal.Parse(str);
                string[] newNum = Adjust(result.ToString(), right1);
                if (right1 > 0 || right2 > 0)
                {
                    return(newNum[0] + "E+" + newNum[1]);
                }
                else
                {
                    return(newNum[0] + "E+" + newNum[1]);
                }
            }
            else
            {
                StringBuilder sb  = new StringBuilder();
                string        str = "0." + sb.Append('0', right2 - right1 - 1) + left1.ToString().Replace(".", "");
                Console.WriteLine(str);
                decimal  result = left2 - decimal.Parse(str);
                string[] newNum = Adjust(result.ToString(), right2);
                if (right1 > 0 || right2 > 0)
                {
                    return(newNum[0] + "E+" + newNum[1]);
                }
                else
                {
                    return(newNum[0] + "E-" + newNum[1]);
                }
            }
        }