예제 #1
0
 public UnitDouble(UnitDouble copy)
 {
     this.UnitType    = copy.UnitType;
     this.Value       = copy.Value;
     this.Unit        = copy.Unit;
     this.DesiredUnit = copy.DesiredUnit;
     this.Converter   = copy.Converter;
     this.Reduce      = copy.Reduce;
 }
예제 #2
0
        public override UnitDouble GetReducedUnit(UnitDouble value)
        {
            var           types         = Enum.GetValues(typeof(DistanceUnits));
            decimal       smallestOver1 = value.Value;
            DistanceUnits smallestOver1Type;

            smallestOver1Type = (DistanceUnits)value.Unit;
            //(DistanceUnits)Enum.Parse(typeof(DistanceUnits), value.Unit);

            // Convert to Imperial if we are requesting imperial
            if (value.UnitType == UnitTypes.DistanceImperial)
            {
                smallestOver1     = this.Convert(smallestOver1, DistanceUnits.Meter, DistanceUnits.Foot);
                smallestOver1Type = DistanceUnits.Foot;
            }

            foreach (DistanceUnits type in types)
            {
                var unitTypes = type.GetAttributeOfType <UnitTypeAttribute>();

                if (unitTypes == null)
                {
                    continue;
                }

                if (unitTypes.Where(p => p.UnitType == value.UnitType).Count() > 0)
                {
                    var convertedValue = this.Convert(smallestOver1, smallestOver1Type, type);

                    // Determine if we need to go to a smaller scale, bigger number
                    if (smallestOver1 < 1)
                    {
                        if (convertedValue > smallestOver1 && convertedValue < 10000)
                        {
                            smallestOver1     = convertedValue;
                            smallestOver1Type = type;
                        }
                    }
                    else // we need to go bigger scale, smaller number
                    {
                        if (convertedValue < smallestOver1 && convertedValue > .5M)
                        {
                            smallestOver1     = convertedValue;
                            smallestOver1Type = type;
                        }
                    }
                }
            }

            return(new UnitDouble(smallestOver1, value.UnitType, smallestOver1Type, this));
        }
예제 #3
0
        private static bool confirmCompatibleTypes(UnitDouble left, UnitDouble right, out UnitConverter convert, out UnitTypes unittype, out Enum unit)
        {
            if (right != null && left.Converter != right.Converter && right.UnitType != UnitTypes.None && left.UnitType != UnitTypes.None)
            {
                convert  = null;
                unittype = UnitTypes.None;
                unit     = null;
                return(false);
            }

            convert  = (left.UnitType == UnitTypes.None && right != null ? right.Converter : left.Converter);
            unittype = (left.UnitType == UnitTypes.None && right != null ? right.UnitType : left.UnitType);
            unit     = (left.UnitType == UnitTypes.None && right != null ? right.Unit : left.Unit);

            return(true);
        }
예제 #4
0
        //public abstract bool GetUnitFromString(string unit, out Enum result);

        public virtual UnitDouble GetReducedUnit(UnitDouble value)
        {
            var     types         = Enum.GetValues(BaseUnit.GetType());
            decimal smallestOver1 = value.Value;
            Enum    smallestOver1Type;

            smallestOver1Type = value.Unit;

            foreach (Enum type in types)
            {
                var unitTypes = type.GetAttributeOfType <UnitTypeAttribute>();

                if (unitTypes == null)
                {
                    continue;
                }

                if (unitTypes.Where(p => p.UnitType == value.UnitType).Count() > 0)
                {
                    var convertedValue = this.Convert(smallestOver1, smallestOver1Type, type);

                    // Determine if we need to go to a smaller scale, bigger number
                    if (smallestOver1 < 1)
                    {
                        if (convertedValue > smallestOver1 && convertedValue < 10000)
                        {
                            smallestOver1     = convertedValue;
                            smallestOver1Type = type;
                        }
                    }
                    else // we need to go bigger scale, smaller number
                    {
                        if (convertedValue < smallestOver1 && convertedValue > .5M)
                        {
                            smallestOver1     = convertedValue;
                            smallestOver1Type = type;
                        }
                    }
                }
            }

            return(new UnitDouble(smallestOver1, value.UnitType, smallestOver1Type, this));
        }
예제 #5
0
 public NumericMathNode(UnitDouble value)
 {
     this.value = value;
 }
예제 #6
0
 public StringFromDouble(UnitDouble copy) : base(copy)
 {
 }
예제 #7
0
 public override UnitDouble GetReducedUnit(UnitDouble value)
 {
     return(value);
 }
예제 #8
0
        public override string ToString()
        {
            var value = new UnitDouble(this);

            if (this.Converter != null && Reduce)
            {
                value = this.Converter.GetReducedUnit(this);
            }
            else if (this.Converter != null && !Reduce)
            {
                value.Value = this.Converter.Convert(value.Value, Converter.BaseUnit, value.DesiredUnit);
                value.Unit  = value.DesiredUnit;
            }

            string formatting = UnitDouble.FORMATTING_STRING_DEFAULT + " ";

            if (Reduce && value.Value > 10000000000000000)
            {
                formatting = "E";
            }

            if (value.UnitType == UnitTypes.Currency)
            {
                formatting  = "###,###,###,###,##0.############ ;-###,###,###,###,##0.############ ";
                value.Value = Math.Round(value.Value, 2);
            }

            if (value.UnitType == UnitTypes.Hexadecimal)
            {
                return("0x" + ((long)value.Value).ToString("X"));
            }
            else if (value.UnitType == UnitTypes.Octal)
            {
                return("0" + Convert.ToString(((long)value.Value), 8));
            }
            else if (value.UnitType == UnitTypes.Binary)
            {
                return("0b" + Convert.ToString(((long)value.Value), 2));
            }

            value.Value = Math.Round(value.Value, 6);
            string unitlabel = String.Empty;

            if (this.Converter != null)
            {
                unitlabel = value.Unit.ToString();


                if (value.Value != 1)
                {
                    var temp = value.Unit.GetAttributeOfType <UnitPluralAttribute>();

                    if (temp != null)
                    {
                        unitlabel = temp.FirstOrDefault().Plural;
                    }
                }
                else
                {
                    var templbl = value.Unit.GetAttributeOfType <DisplayAttribute>();
                    if (templbl != null)
                    {
                        unitlabel = templbl.FirstOrDefault().Display;
                    }
                }
            }

            return((value.Value.ToString(formatting) + (this.Converter != null ? unitlabel : "")).Trim());
        }