C#超大数字运算

本文介绍如何处理C#中超过long类型的超大数字运算。提供了两个类,分别用于整数和浮点数的大数运算,适用于需要进行大数运算的场景,如:111111111111111111111111111111111111*122222123123122222222221222221。这两个类经过修改,适配了.NET 3.5,方便在Unity中使用。
摘要由CSDN通过智能技术生成

这里记两个计算大数字运算的类(这是两个类是反编译,是之前用了.net4.0的一些特性,用.net3.5导入unity报错,就反编译改了下,贴在这里以后能用着也好找)。实际使用不推荐,推荐使用System.Numerics命名空间下的。
说说使用,当我们要运算的数值超过了double,long的时候,或者计算是string的数字的时候,例如:111111111111111111111111111111111111*122222123123122222222221222221
这么一个等式。

第一个类整数型

using System;

namespace Sqrt
{
  internal class ComputeStringInt
  {
    public string Body = "0";

    public static readonly ComputeStringInt Zero  = new ComputeStringInt("0");

    public int Length
    {
      get
      {
        if ((int) this.Body[0] == 43 || (int) this.Body[0] == 45)
          return this.Body.Length - 1;
        return this.Body.Length;
      }
    }

    public int this[int index]
    {
      get
      {
        if ((int) this.Body[0] == 43 || (int) this.Body[0] == 45)
          return (int) this.Body[index + 1] - 48;
        return (int) this.Body[index] - 48;
      }
    }

    public bool IsPositive
    {
      get
      {
        return (int) this.Body[0] != 45 || !(new ComputeStringInt(this.Body.Substring(1)) != (ComputeStringInt) 0L);
      }
    }

    public ComputeStringInt()
    {
    }

    public ComputeStringInt(string numStr)
    {
      if (string.IsNullOrEmpty(numStr))
        this.Body = "0";
      else
        this.Body = this.Trim(numStr);
    }

    public static implicit operator string(ComputeStringInt source)
    {
      return source.Body;
    }

    public static implicit operator ComputeStringInt(long real)
    {
      return new ComputeStringInt(real.ToString());
    }

    public static implicit operator ComputeStringInt(string real)
    {
      return new ComputeStringInt(real);
    }

    public static bool operator >(ComputeStringInt left, ComputeStringInt right)
    {
      if (left.IsPositive && !right.IsPositive && left.Absolute().Body != "0" && right.Absolute().Body != "0")
        return true;
      if (left.IsPositive && right.IsPositive)
      {
        if (left.Length > right.Length)
          return true;
        if (left.Length != right.Length)
          return false;
        for (int index = 0; index < left.Length; ++index)
        {
          if (left[index] > right[index])
            return true;
          if (left[index] < right[index])
            return false;
        }
      }
      else if (!left.IsPositive && !right.IsPositive)
      {
        if (left.Length < right.Length)
          return true;
        if (left.Length != right.Length)
          return false;
        for (int index = 0; index < left.Length; ++index)
        {
          if (left[index] < right[index])
            return true;
          if (left[index] > right[index])
            return false;
        }
      }
      else if (left.IsPositive && !right.IsPositive && (left.Absolute().Body == "0" && right.Absolute().Body != "0" || left.Absolute().Body != "0" && right.Absolute().Body == "0"))
        return true;
      return false;
    }

    public static bool operator <(ComputeStringInt left, ComputeStringInt right)
    {
      return right > left;
    }

    public static bool operator >=(ComputeStringInt left, ComputeStringInt right)
    {
      return left > right || left == right;
    }

    public static bool operator <=(ComputeStringInt left, ComputeStringInt right)
    {
      return left < right || left == right;
    }

    public static bool operator ==(ComputeStringInt left, ComputeStringInt right)
    {
      return !(left > right) && !(left < right);
    }

    public static bool operator !=(ComputeStringInt left, ComputeStringInt right)
    {
      return left > right || left < right;
    }

    public static bool operator >(ComputeStringInt left, object right)
    {
      return left > new ComputeStringInt(right.ToString());
    }

    public static bool operator >(object left, ComputeStringInt right)
    {
      return new ComputeStringInt(left.ToString()) > right;
    }

    public static bool operator <(ComputeStringInt left, object right)
    {
      return left < new ComputeStringInt(right.ToString());
    }

    public static bool operator <(object left, ComputeStringInt right)
    {
      return new ComputeStringInt(left.ToString()) < right;
    }

    public static bool operator >=(ComputeStringInt left, object right)
    {
      return left >= new ComputeStringInt(right.ToString());
    }

    public static bool operator >=(object left, ComputeStringInt right)
    {
      return new ComputeStringInt(left.ToString()) >= right;
    }

    public static bool operator <=(ComputeStringInt left, object right)
    {
      return left <= new ComputeStringInt(right.ToString());
    }

    public static bool operator <=(object left, ComputeStringInt right)
    {
      return new ComputeStringInt(left.ToString()) <= right;
    }

    public static bool operator ==(ComputeStringInt left, object right)
    {
      return left == new ComputeStringInt(right.ToString());
    }

    public static bool operator ==(object left, ComputeStringInt right)
    {
      return new ComputeStringInt(left.ToString()) == right;
    }

    public static bool operator !=(ComputeStringInt left, object right)
    {
      return left != new ComputeStringInt(right.ToString());
    }

    public static bool operator !=(object left, ComputeStringInt right)
    {
      return new ComputeStringInt(left.ToString()) != right;
    }

    public static ComputeStringInt operator +(ComputeStringInt left, ComputeStringInt right)
    {
      ComputeStringInt computeStringInt1 = new ComputeStringInt();
      if (left.IsPositive && right.IsPositive)
      {
        int length = right.Length;
        if (left.Length > right.Length)
          length = left.Length;
        int num1 = 0;
        for (int index = 0; index < length; ++index)
        {
          int num2 = index >= left.Length || index >= right.Length ? (index < left.Length ? (index < right.Length ? 0 : left[left.Length - 1 - index] + num1) : right[right.Length - 1 - index] + num1) : left[left.Length - 1 - index] + right[right.Length - 1 - index] + num1;
          num1 = num2 / 10;
          int num3;
          if (index == 0)
          {
            ComputeStringInt computeStringInt2 = computeStringInt1;
            num3 = num2 % 10;
            string str = num3.ToString();
            computeStringInt2.Body = str;
          }
          else
          {
            ComputeStringInt computeStringInt2 = computeStringInt1;
            string str1 = computeStringInt1.Body;
            int startIndex = 0;
            num3 = num2 % 10;
            string str2 = num3.ToString();
            string str3 = str1.Insert(startIndex, str2);
            computeStringInt2.Body = str3;
          }
        }
        computeStringInt1.Body = computeStringInt1.Body.Insert(0, num1.ToString());
      }
      else if (left.IsPositive && !right.IsPositive)
        computeStringInt1 = left - new ComputeStringInt((string) right.Absolute());
      else if (!left.IsPositive && right.IsPositive)
        computeStringInt1 = right - new ComputeStringInt((string) left.Absolute());
      else if (!left.IsPositive && !right.IsPositive)
        computeStringInt1 = left - new ComputeStringInt((string) right.Absolute());
      computeStringInt1.Trim();
      return computeStringInt1;
    }

    public static ComputeStringInt operator -(ComputeStringInt left, ComputeStringInt right)
    {
      ComputeStringInt computeStringInt = new ComputeStringInt();
      if (left == right)
        return new ComputeStringInt("0");
      if (left > ComputeStringInt.Zero && right == ComputeStringInt.Zero)
        return left;
      if (left == ComputeStringInt.Zero && right < ComputeStringInt.Zero)
        return new ComputeStringInt((string) right.Absolute());
      if (left == ComputeStringInt.Zero && right > ComputeStringInt.Zero)
        return new ComputeStringInt(right.Body.Insert(0, "-"));
      if (left > ComputeStringInt.Zero && right > ComputeStringInt.Zero)
      {
        if (left > right)
        {
          int num1 = 0;
          for (int index = 0; index < left.Length; ++index)
          {
            int num2 = index >= left.Length || index >= right.Length ? (index < right.Length ? 0 : left[left.Length - 1 - index] - num1) : left[left.Length - 1 - index] - right[right.Length - 1 - index] - num1;
            num1 = num2 / 10;
            if (num2 < 0)
            {
              num1 = 1;
              num2 = 10 + num2;
            }
            computeStringInt.Body = index != 0 ? computeStringInt.Body.Insert(0, num2.ToString()) : num2.ToString();
          }
        }
        else if (left < right)
        {
          computeStringInt = right - left;
          computeStringInt.Body = computeStringInt.Body.Insert(0, "-");
        }
      }
      else if (left < ComputeStringInt.Zero && right < ComputeStringInt.Zero)
        computeStringInt = new ComputeStringInt((string) right.Absolute()) - new ComputeStringInt((string) left.Absolute());
      else if (left > ComputeStringInt.Zero && right < ComputeStringInt.Zero)
        computeStringInt = left + new ComputeStringInt((string) right.Absolute());
      else if (left < ComputeStringInt.Zero && right > ComputeStringInt.Zero)
      {
        computeStringInt = new ComputeStringInt((string) left.Absolute()) + right;
        computeStringInt.Body = computeStringInt.Body.Insert(0, "-");
      }
      computeStringInt.Trim();
      return computeStringInt;
    }

    public static ComputeStringInt operator *(ComputeStringInt left, ComputeStringInt right)
    {
      ComputeStringInt computeStringInt = new ComputeStringInt();
      if (left == ComputeStringInt.Zero || right == ComputeStringInt.Zero)
        return ComputeStringInt.Zero;
      if (left.IsPositive && right.IsPositive)
      {
        for (int index1 = right.Length - 1; index1 >= 0; --index1)
        {
          int num1 = 0;
          string numStr = "";
          for (int index2 = left.Length - 1; index2 >= 0; --index2)
          {
            int num2 = right.Absolute()[index1] * left.Absolute()[index2] + num1;
            numStr = numStr.Insert(0, (num2 % 10).ToString());
            num1 = num2 / 10;
          }
          if ((uint) num1 > 0U)
            numStr = numStr.Insert(0, num1.ToString());
          for (int index2 = 0; index2 < right.Length - 1 - index1; ++index2)
            numStr += "0";
          computeStringInt += new ComputeStringInt(numStr);
        }
      }
      else if (!left.IsPositive && right.IsPositive || left.IsPositive && !right.IsPositive)
      {
        computeStringInt = new ComputeStringInt((string) left.Absolute()) * new ComputeStringInt((string) right.Absolute());
        computeStringInt.Body = computeStringInt.Body.Insert(0, "-");
      }
      else if (!left.IsPositive && !right.IsPositive)
        computeStringInt = new ComputeStringInt((string) left.Absolute()) * new ComputeStringInt((string) right.Absolute());
      computeStringInt.Trim();
      return computeStringInt;
    }

    public static ComputeStringInt operator /(ComputeStringInt left, ComputeStringInt right)
    {
      ComputeStringInt computeStringInt1 = new ComputeStringInt();
      if (right == ComputeStringInt.Zero)
        throw new Exception("除零错误!");
      if (left == ComputeStringInt.Zero)
        return ComputeStringInt.Zero;
      if (right == new ComputeStringInt("1"))
        return left;
      if (left.IsPositive && right.IsPositive)
      {
        if (left < right)
        {
          computeStringInt1 = ComputeStringInt.Zero;
        }
        else
        {
          string numStr = "";
          ComputeStringInt computeStringInt2 = new ComputeStringInt(left.Body.Substring(0, right.Length - 1));
          for (int index = right.Length - 1; index < left.Length; ++index)
          {
            computeStringInt2 = computeStringInt2 * new ComputeStringInt("10") + new ComputeStringInt(left[index].ToString());
            computeStringInt2.Trim();
            int num = 0;
            for (num = 0; num < 10; ++num)
            {
              ComputeStringInt computeStringInt3 = right * new ComputeStringInt(num.ToString());
              if (computeStringInt3 > computeStringInt2)
              {
                numStr +=  (num - 1);
                computeStringInt2 -= right * new ComputeStringInt((num - 1).ToString());
                break;
              }
              if (computeStringInt3 == computeStringInt2)
              {
                numStr += num;
                computeStringInt2 = ComputeStringInt.Zero;
                break;
              }
            }
            if (num == 10)
            {
              numStr +=(num - 1);
              computeStringInt2 -= right * new ComputeStringInt((num - 1).ToString());
            }
          }
          computeStringInt1 = new ComputeStringInt(numStr);
        }
      }
      else if (!left.IsPositive && right.IsPositive || left.IsPositive && !right.IsPositive)
      {
        computeStringInt1 = new ComputeStringInt((string) left.Absolute()) / new ComputeStringInt((string) right.Absolute());
        computeStringInt1.Body = computeStringInt1.Body.Insert(0, "-");
      }
      else if (!left.IsPositive && !right.IsPositive)
        computeStringInt1 = new ComputeStringInt((string) left.Absolute()) / new ComputeStringInt((string) right.Absolute());
      computeStringInt1.Trim();
      return computeStringInt1;
    }

    public static ComputeStringInt operator %(ComputeStringInt left, ComputeStringInt right)
    {
      ComputeStringInt computeStringInt1 = new ComputeStringInt();
      ComputeStringInt computeStringInt2 = left - left / right * right;
      computeStringInt2.Trim();
      return computeStringInt2;
    }

    public static ComputeStringInt operator +(ComputeStringInt left, object right)
    {
      return left + new ComputeStringInt(right.ToString());
    }

    public static ComputeStringInt operator +(object left, ComputeStringInt right)
    {
      return new ComputeStringInt(left.ToString()) + right;
    }

    public static ComputeStringInt operator -(ComputeStringInt left, object right)
    {
      return left - new ComputeStringInt(right.ToString());
    }

    public static ComputeStringInt operator -(object left, ComputeStringInt right)
    {
      return new ComputeStringInt(left.ToString()) - right;
    }

    public static ComputeStringInt operator *(ComputeStringInt left, object right)
    {
      return left * new ComputeStringInt(right.ToString());
    }

    public static ComputeStringInt operator *(object left, ComputeStringInt right)
    {
      return new ComputeStringInt(left.ToString()) * right;
    }

    public static ComputeStringInt operator /(ComputeStringInt left, object right)
    {
      return left / new ComputeStringInt(right.ToString());
    }

    public static ComputeStringInt operator /(object left, ComputeStringInt right)
    {
      return new ComputeStringInt(left.ToString()) / right;
    }

    public static ComputeStringInt operator %(ComputeStringInt left, object right)
    {
      return left % new ComputeStringInt(right.ToString());
    }

    public static ComputeStringInt operator %(object left, ComputeStringInt right)
    {
      return new ComputeStringInt(left.ToString()) % right;
    }

    public ComputeStringInt Absolute()
    {
      if ((int) this.Body[0] == 43 || (int) this.Body[0] == 45)
        return new ComputeStringInt(this.Body.Substring(1));
      return this;
    }

    public void Trim()
    {
      this.Body = this.Trim(this.Body);
    }

    public string Trim(string numStr)
    {
      string str1 = numStr.Trim();
      if ((int) str1[0] == 43)
        str1 = str1.Substring(1);
      string str2 = str1;
      if ((int) str1[0] == 45)
        str2 = str1.Substring(1);
      int index = 0;
      while (index < str2.Length && (int) str2[index] == 48)
        ++index;
      if (index == str2.Length)
        return "0";
      bool flag = true;
      if ((int) str1[0] == 45)
        flag = false;
      int startIndex = 0;
      while (startIndex < str2.Length && (int) str2[startIndex] == 48)
        ++startIndex;
      string str3 = str2.Substring(startIndex);
      if (!flag)
        str3 = str3.Insert(0, "-");
      return str3;
    }

    public override string ToString()
    {
      return this.Body;
    }

    public ComputeStringFloat ToComputeStringFloat()
    {
      return new ComputeStringFloat(this.Body);
    }

    public override bool Equals(object obj)
    {
      return base.Equals(obj);
    }

    public override int GetHashCode()
    {
      return base.GetHashCode();
    }
  }
}


第二类浮点类型

sing System.Text.RegularExpressions;

namespace Sqrt
{
    internal class ComputeStringFloat : ComputeStringInt
    {
        public static int DividResaultPrecision = 50;
        private long DotIndex = 0;
        private string BodyNoDotNoSignal = "0";

        public string IntPart
        {
            get
            {
                if (this.DotIndex <= 0L)
                    return "0";
                return this.BodyNoDotNoSignal.Substring(0, (int)this.DotIndex);
            }
        }

        public string FloatPart
        {
            get
            {
                if (this.DotIndex == (long)this.BodyNoDotNoSignal.Length)
                    return "0";
                if (this.DotIndex == 0L)
                    return this.BodyNoDotNoSignal;
                if (this.DotIndex >= 0L)
                    return this.BodyNoDotNoSignal.Substring((int)this.DotIndex);
                string str = this.BodyNoDotNoSignal;
                for (int index = 0; (long)index < -this.DotIndex; ++index)
                    str = str.Insert(0, "0");
                return str;
            }
        }

        public int IntPartLength
        {
            get
            {
                return this.IntPart.Length;
            }
        }

        public int FloatPartLength
        {
            get
            {
                if (this.FloatPart.Length == 1 && (int)this.FloatPart[0] == 48)
                    return 0;
                return this.FloatPart.Length;
            }
        }

        public ComputeStringFloat()
        {
            this.DotIndex = 1L;
            this.Body = "0";
            this.BodyNoDotNoSignal = "0";
        }

        public ComputeStringFloat(string body)
        {
            if (string.IsNullOrEmpty(body))
                this.Body = "0";
            else
                this.Body = this.Trim(body);
            Match match = Regex.Match(this.Body, "[1-9]");
            int num = this.Body.IndexOf('.');
            this.DotIndex = num < 0 ? (long)(this.Body.Length - match.Index) : (num <= match.Index ? (long)(num - match.Index + 1) : (long)(num - match.Index));
            this.BodyNoDotNoSignal = this.Body.Substring(match.Index);
            int startIndex = this.BodyNoDotNoSignal.IndexOf('.');
            if (startIndex >= 0)
                this.BodyNoDotNoSignal = this.BodyNoDotNoSignal.Remove(startIndex, 1);
            this.BodyNoDotNoSignal = base.Trim(this.BodyNoDotNoSignal);
        }

        public static implicit operator string(ComputeStringFloat source)
        {
            return source.Body;
        }

        public static implicit operator ComputeStringFloat(long real)
        {
            return new ComputeStringFloat(real.ToString());
        }

        public static implicit operator ComputeStringFloat(string real)
        {
            return new ComputeStringFloat(real);
        }

        public static bool operator >(ComputeStringFloat left, ComputeStringFloat right)
        {
            if (left.IsPositive && right.IsPositive)
            {
                if (new ComputeStringInt(left.IntPart) > new ComputeStringInt(right.IntPart))
                    return true;
                int length1 = left.FloatPart.Length;
                int length2 = right.FloatPart.Length;
                string floatPart1 = left.FloatPart;
                string floatPart2 = right.FloatPart;
                if (length1 > length2)
                {
                    for (int index = length2; index < length1; ++index)
                        floatPart2 += "0";
                }
                else if (length1 < length2)
                {
                    for (int index = length1; index < length2; ++index)
                        floatPart1 += "0";
                }
                return new ComputeStringInt(floatPart1) > new ComputeStringInt(floatPart2);
            }
            return left.IsPositive && !right.IsPositive || (left.IsPositive || !right.IsPositive) && (!left.IsPositive && !right.IsPositive && new ComputeStringFloat((string)right.Absolute()) > new ComputeStringFloat((string)left.Absolute()));
        }

        public static bool operator <(ComputeStringFloat left, ComputeStringFloat right)
        {
            return right > left;
        }

        public static bool operator ==(ComputeStringFloat left, ComputeStringFloat right)
        {
            return !(left > right) && !(left < right);
        }

        public static bool operator !=(ComputeStringFloat left, ComputeStringFloat right)
        {
            return left > right || left < right;
        }

        public static bool operator >=(ComputeStringFloat left, ComputeStringFloat right)
        {
            return left < right || left == right;
        }

        public static bool operator <=(ComputeStringFloat left, ComputeStringFloat right)
        {
            return left > right || left == right;
        }

        public static bool operator >(ComputeStringFloat left, object right)
        {
            return left > new ComputeStringFloat(right.ToString());
        }

        public static bool operator >(object left, ComputeStringFloat right)
        {
            return new ComputeStringFloat(left.ToString()) > right;
        }

        public static bool operator <(ComputeStringFloat left, object right)
        {
            return left < new ComputeStringFloat(right.ToString());
        }

        public static bool operator <(object left, ComputeStringFloat right)
        {
            return new ComputeStringFloat(left.ToString()) < right;
        }

        public static bool operator ==(ComputeStringFloat left, object right)
        {
            return left == new ComputeStringFloat(right.ToString());
        }

        public static bool operator ==(object left, ComputeStringFloat right)
        {
            return new ComputeStringFloat(left.ToString()) == right;
        }

        public static bool operator !=(ComputeStringFloat left, object right)
        {
            return left != new ComputeStringFloat(right.ToString());
        }

        public static bool operator !=(object left, ComputeStringFloat right)
        {
            return new ComputeStringFloat(left.ToString()) != right;
        }

        public static bool operator >=(ComputeStringFloat left, object right)
        {
            return left >= new ComputeStringFloat(right.ToString());
        }

        public static bool operator >=(object left, ComputeStringFloat right)
        {
            return new ComputeStringFloat(left.ToString()) >= right;
        }

        public static bool operator <=(ComputeStringFloat left, object right)
        {
            return left <= new ComputeStringFloat(right.ToString());
        }

        public static bool operator <=(object left, ComputeStringFloat right)
        {
            return new ComputeStringFloat(left.ToString()) <= right;
        }

        public static ComputeStringFloat operator +(ComputeStringFloat left, ComputeStringFloat right)
        {
            if (left == (ComputeStringFloat)0L)
                return right;
            if (right == (ComputeStringFloat)0L)
                return left;
            string numStr1 = left.Body;
            int startIndex1 = numStr1.IndexOf('.');
            if (startIndex1 >= 0)
                numStr1 = numStr1.Remove(startIndex1, 1);
            string numStr2 = right.Body;
            int startIndex2 = numStr2.IndexOf('.');
            if (startIndex2 >= 0)
                numStr2 = numStr2.Remove(startIndex2, 1);
            int length1 = left.FloatPart.Length;
            if (right.FloatPart.Length > length1)
            {
                length1 = right.FloatPart.Length;
                if (left.FloatPart == "0")
                {
                    for (int length2 = left.FloatPart.Length; length2 <= length1; ++length2)
                        numStr1 += "0";
                }
                else
                {
                    for (int length2 = left.FloatPart.Length; length2 < length1; ++length2)
                        numStr1 += "0";
                }
            }
            else if (right.FloatPart == "0")
            {
                for (int length2 = right.FloatPart.Length; length2 <= length1; ++length2)
                    numStr2 += "0";
            }
            else
            {
                for (int length2 = right.FloatPart.Length; length2 < length1; ++length2)
                    numStr2 += "0";
            }
            ComputeStringInt computeStringInt = new ComputeStringInt(numStr1) + new ComputeStringInt(numStr2);
            if (computeStringInt.Length == length1)
                return (ComputeStringFloat)computeStringInt.Body.Insert(computeStringInt.Body.Length - length1, "0.");
            if (computeStringInt.Length >= length1)
                return (ComputeStringFloat)computeStringInt.Body.Insert(computeStringInt.Body.Length - length1, ".");
            string str = (string)computeStringInt.Absolute();
            for (int length2 = str.Length; length2 < length1; ++length2)
                str = "0" + str;
            if (computeStringInt.IsPositive)
                return (ComputeStringFloat)str.Insert(0, "0.");
            return (ComputeStringFloat)str.Insert(0, "-0.");
        }

        public static ComputeStringFloat operator -(ComputeStringFloat left, ComputeStringFloat right)
        {
            if (right == (ComputeStringFloat)0L)
                return left;
            if (right.IsPositive)
                return left + (ComputeStringFloat)right.Absolute().Body.Insert(0, "-");
            return left + right.Absolute();
        }

        public static ComputeStringFloat operator *(ComputeStringFloat left, ComputeStringFloat right)
        {
            string numStr1 = left.Body;
            int startIndex1 = numStr1.IndexOf('.');
            if (startIndex1 >= 0)
                numStr1 = numStr1.Remove(startIndex1, 1);
            string numStr2 = right.Body;
            int startIndex2 = numStr2.IndexOf('.');
            if (startIndex2 >= 0)
                numStr2 = numStr2.Remove(startIndex2, 1);
            int floatPartLength1 = left.FloatPartLength;
            if (right.FloatPartLength > floatPartLength1)
            {
                floatPartLength1 = right.FloatPartLength;
                for (int floatPartLength2 = left.FloatPartLength; floatPartLength2 < floatPartLength1; ++floatPartLength2)
                    numStr1 += "0";
            }
            else if (right.FloatPartLength < floatPartLength1)
            {
                for (int floatPartLength2 = right.FloatPartLength; floatPartLength2 < floatPartLength1; ++floatPartLength2)
                    numStr2 += "0";
            }
            ComputeStringInt computeStringInt = new ComputeStringInt(numStr1) * new ComputeStringInt(numStr2);
            int num = floatPartLength1 + floatPartLength1;
            if (computeStringInt.Length == num)
                return (ComputeStringFloat)computeStringInt.Body.Insert(computeStringInt.Body.Length - num, "0.");
            if (computeStringInt.Length >= num)
                return (ComputeStringFloat)computeStringInt.Body.Insert(computeStringInt.Body.Length - num, ".");
            string str = (string)computeStringInt.Absolute();
            for (int length = str.Length; length < num; ++length)
                str = "0" + str;
            if (computeStringInt.IsPositive)
                return (ComputeStringFloat)str.Insert(0, "0.");
            return (ComputeStringFloat)str.Insert(0, "-0.");
        }

        public static ComputeStringFloat operator /(ComputeStringFloat left, ComputeStringFloat right)
        {
            string numStr1 = left.Body;
            int startIndex1 = numStr1.IndexOf('.');
            if (startIndex1 >= 0)
                numStr1 = numStr1.Remove(startIndex1, 1);
            string numStr2 = right.Body;
            int startIndex2 = numStr2.IndexOf('.');
            if (startIndex2 >= 0)
                numStr2 = numStr2.Remove(startIndex2, 1);
            int floatPartLength1 = left.FloatPartLength;
            if (right.FloatPartLength > floatPartLength1)
            {
                int floatPartLength2 = right.FloatPartLength;
                for (int floatPartLength3 = left.FloatPartLength; floatPartLength3 < floatPartLength2; ++floatPartLength3)
                    numStr1 += "0";
            }
            else if (right.FloatPartLength < floatPartLength1)
            {
                for (int floatPartLength2 = right.FloatPartLength; floatPartLength2 < floatPartLength1; ++floatPartLength2)
                    numStr2 += "0";
            }
            for (int index = 0; index < ComputeStringFloat.DividResaultPrecision; ++index)
                numStr1 += "0";
            ComputeStringInt computeStringInt = new ComputeStringInt(numStr1) / new ComputeStringInt(numStr2);
            int num = ComputeStringFloat.DividResaultPrecision;
            if (computeStringInt.Length == num)
                return (ComputeStringFloat)computeStringInt.Body.Insert(computeStringInt.Body.Length - num, "0.");
            if (computeStringInt.Length >= num)
                return (ComputeStringFloat)computeStringInt.Body.Insert(computeStringInt.Body.Length - num, ".");
            string str = (string)computeStringInt.Absolute();
            for (int length = str.Length; length < num; ++length)
                str = "0" + str;
            if (computeStringInt.IsPositive)
                return (ComputeStringFloat)str.Insert(0, "0.");
            return (ComputeStringFloat)str.Insert(0, "-0.");
        }

        public static ComputeStringFloat operator +(ComputeStringFloat left, object right)
        {
            return left + new ComputeStringFloat(right.ToString());
        }

        public static ComputeStringFloat operator +(object left, ComputeStringFloat right)
        {
            return new ComputeStringFloat(left.ToString()) + right;
        }

        public static ComputeStringFloat operator -(ComputeStringFloat left, object right)
        {
            return left - new ComputeStringFloat(right.ToString());
        }

        public static ComputeStringFloat operator -(object left, ComputeStringFloat right)
        {
            return new ComputeStringFloat(left.ToString()) - right;
        }

        public static ComputeStringFloat operator *(ComputeStringFloat left, object right)
        {
            return left * new ComputeStringFloat(right.ToString());
        }

        public static ComputeStringFloat operator *(object left, ComputeStringFloat right)
        {
            return new ComputeStringFloat(left.ToString()) * right;
        }

        public static ComputeStringFloat operator /(ComputeStringFloat left, object right)
        {
            return left / new ComputeStringFloat(right.ToString());
        }

        public static ComputeStringFloat operator /(object left, ComputeStringFloat right)
        {
            return new ComputeStringFloat(left.ToString()) / right;
        }

        public ComputeStringFloat Absolute()
        {
            if ((int)this.Body[0] == 43 || (int)this.Body[0] == 45)
                return new ComputeStringFloat(this.Body.Substring(1));
            return this;
        }

        public new string Trim(string numStr)
        {
            string str1 = numStr.Trim();
            if ((int)str1[0] == 43)
                str1 = str1.Substring(1);
            int num = str1.IndexOf('.');
            if (num >= 0)
            {
                for (int length = str1.Length - 1; length >= num; --length)
                {
                    if ((int)str1[length] != 48)
                    {
                        str1 = (int)str1[length] != 46 ? str1.Substring(0, length + 1) : str1.Substring(0, length);
                        break;
                    }
                }
            }
            bool flag = false;
            string str2 = str1;
            string str3 = "";
            char ch;
            for (int index = 0; index < str2.Length; ++index)
            {
                if (flag)
                {
                    string str4 = str3;
                    ch = str2[index];
                    string str5 = ch.ToString();
                    str3 = str4 + str5;
                }
                else if ((int)str2[index] == 45)
                {
                    string str4 = str3;
                    ch = str2[index];
                    string str5 = ch.ToString();
                    str3 = str4 + str5;
                }
                else if ((int)str2[index] == 48)
                {
                    if (index + 1 < str2.Length && (int)str2[index + 1] == 46 || index + 1 >= str2.Length)
                    {
                        string str4 = str3;
                        ch = str2[index];
                        string str5 = ch.ToString();
                        str3 = str4 + str5;
                        flag = true;
                    }
                }
                else
                {
                    string str4 = str3;
                    ch = str2[index];
                    string str5 = ch.ToString();
                    str3 = str4 + str5;
                    flag = true;
                }
            }
            if (str3.Length == 2 && (int)str3[0] == 45 && (int)str3[1] == 48)
                str3 = "0";
            return str3;
        }

        public override string ToString()
        {
            return this.Body;
        }

        public ComputeStringInt ToComputeStringInt()
        {
            string numStr = this.IntPart;
            if (!this.IsPositive)
                numStr = numStr.Insert(0, "-");
            return new ComputeStringInt(numStr);
        }

        public override bool Equals(object obj)
        {
            return base.Equals(obj);
        }

        public override int GetHashCode()
        {
            return base.GetHashCode();
        }
    }
}

用法和注意事项:

    private string s1 = "-1";
    private string s2 = "1111111111111222.2222";

    private ComputeStringInt c1 = "2";
    private ComputeStringFloat c2 = "2.3";
    // Use this for initialization
    void Test()
    {
        //注意类型
        Debug.Log(new ComputeStringFloat(s1) / 3);
        ComputeStringFloat.DividResaultPrecision = 3;//浮点的精度 注意是全局的
        Debug.Log((ComputeStringFloat)s2 / 3);
        Debug.Log(c1 / s1);

        Debug.Log(c1 * c2.IntPart);
        // Debug.LogError(c1 * (ComputeStringInt)c2); //注意不能这么用,因为.的ASCII码为46,由于字符数字转换为数字要减去48,那么参与运算的就是2(-2)3,所以结果是错误的!


    }
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值