Beispiel #1
0
        public void Format()
        {
            IdleCurrency r = new IdleCurrency(Value, Exp);

            r     = Format(r);
            Value = r.Value;
            Exp   = r.Exp;
        }
Beispiel #2
0
        public static IdleCurrency Abs(IdleCurrency value)
        {
            if (value.Sign < 0)
            {
                return(-value);
            }

            return(value);
        }
Beispiel #3
0
        public static IdleCurrency Multiply(IdleCurrency left, IdleCurrency right)
        {
            IdleCurrency bd = new IdleCurrency(
                left.Value * right.Value,
                left.Exp + right.Exp);

            bd.Simplify();
            return(bd);
        }
Beispiel #4
0
        public static IdleCurrency Divide(IdleCurrency dividend, IdleCurrency divisor)
        {
            if (divisor == 0)
            {
                throw new DivideByZeroException();
            }

            IdleCurrency bd = new IdleCurrency(
                dividend.Value / divisor.Value,
                dividend.Exp - divisor.Exp);

            bd.Simplify();
            return(bd);
        }
Beispiel #5
0
 public static IdleCurrency operator +(IdleCurrency a, IdleCurrency b)
 {
     if (a.Exp == b.Exp)
     {
         IdleCurrency m = new IdleCurrency();
         m.Exp   = a.Exp;
         m.Value = a.Value + b.Value;
         m.Simplify();
         return(m);
     }
     else if (a.Exp > b.Exp)
     {
         // a is bigger
         int deltaExp = a.Exp - b.Exp;
         if (deltaExp <= 16)
         {
             double       bX = b.Value / Math.Pow(10, (double)deltaExp);
             IdleCurrency m  = new IdleCurrency();
             m.Exp   = a.Exp;
             m.Value = a.Value + bX;
             m.Simplify();
             return(m);
         }
         else
         {
             return(a);
         }
     }
     else
     {
         // b is bigger
         int deltaExp = b.Exp - a.Exp;
         if (deltaExp <= 16)
         {
             double       aX = a.Value / Math.Pow(10, (double)deltaExp);
             IdleCurrency m  = new IdleCurrency();
             m.Exp   = b.Exp;
             m.Value = b.Value + aX;
             m.Simplify();
             return(m);
         }
         else
         {
             return(b);
         }
     }
 }
Beispiel #6
0
        public static IdleCurrency Format(IdleCurrency a)
        {
            IdleCurrency r  = a;
            int          p  = r.Exp;
            double       rV = r.Value;

            if (rV < 1 && p <= 3)
            {
                rV = rV * Math.Pow(10, (double)p);
                p  = 0;
            }

            while (rV < 100 && p > 3)
            {
                rV = rV * 10;
                p--;
            }

            if (p < 3)
            {
                rV = rV * Math.Pow(10, (double)p);
                p  = 0;
            }
            else
            {
                if (p % 3 != 0)
                {
                    rV = rV * Math.Pow(10, (double)(p % 3));
                    p -= (p % 3);
                }

                if (rV >= 1000)
                {
                    rV = rV / 1000;
                    p += 3;
                }
            }

            r.Value = rV;
            r.Exp   = p;

            return(r);
        }
Beispiel #7
0
        public static int Compare(IdleCurrency left, IdleCurrency right)
        {
            left.Format();
            right.Format();
            int leftSign  = left.Sign;
            int rightSign = right.Sign;

            // Compare signs
            if (leftSign == 0 && rightSign == 0)
            {
                return(0);
            }

            if (leftSign >= 0 && rightSign < 0)
            {
                return(1);
            }

            if (leftSign < 0 && rightSign >= 0)
            {
                return(-1);
            }

            // Compare exponents
            if (left.Exp > right.Exp)
            {
                return(1);
            }

            if (left.Exp < right.Exp)
            {
                return(-1);
            }

            return(left.Value.CompareTo(right.Value));
        }
Beispiel #8
0
 public static IdleCurrency Subtract(IdleCurrency left, IdleCurrency right)
 {
     return(left - right);
 }
Beispiel #9
0
 public static IdleCurrency Add(IdleCurrency left, IdleCurrency right)
 {
     return(left + right);
 }
Beispiel #10
0
 public int CompareTo(IdleCurrency value)
 {
     return(Compare(this, value));
 }
Beispiel #11
0
        public static int Compare(IdleCurrency left, object right)
        {
            if (right is IdleCurrency)
            {
                return(Compare(left, (IdleCurrency)right));
            }

            if (right is bool)
            {
                return(Compare(left, new IdleCurrency((bool)right)));
            }

            if (right is byte)
            {
                return(Compare(left, new IdleCurrency((byte)right)));
            }

            if (right is char)
            {
                return(Compare(left, new IdleCurrency((char)right)));
            }

            if (right is decimal)
            {
                return(Compare(left, new IdleCurrency((decimal)right)));
            }

            if (right is double)
            {
                return(Compare(left, new IdleCurrency((double)right)));
            }

            if (right is short)
            {
                return(Compare(left, new IdleCurrency((short)right)));
            }

            if (right is int)
            {
                return(Compare(left, new IdleCurrency((int)right)));
            }

            if (right is long)
            {
                return(Compare(left, new IdleCurrency((long)right)));
            }

            if (right is sbyte)
            {
                return(Compare(left, new IdleCurrency((sbyte)right)));
            }

            if (right is float)
            {
                return(Compare(left, new IdleCurrency((float)right)));
            }

            if (right is ushort)
            {
                return(Compare(left, new IdleCurrency((ushort)right)));
            }

            if (right is uint)
            {
                return(Compare(left, new IdleCurrency((uint)right)));
            }

            if (right is ulong)
            {
                return(Compare(left, new IdleCurrency((ulong)right)));
            }

            throw new ArgumentException();
        }
Beispiel #12
0
        public static IdleCurrency operator /(int a, IdleCurrency b)
        {
            IdleCurrency A = new IdleCurrency(a);

            return(A / b);
        }
Beispiel #13
0
        public static IdleCurrency operator /(IdleCurrency a, int b)
        {
            IdleCurrency B = new IdleCurrency(b);

            return(a / B);
        }
Beispiel #14
0
        public static IdleCurrency operator *(float a, IdleCurrency b)
        {
            IdleCurrency A = new IdleCurrency(a);

            return(A * b);
        }
Beispiel #15
0
        public static IdleCurrency operator *(IdleCurrency a, float b)
        {
            IdleCurrency B = new IdleCurrency(b);

            return(a * B);
        }