Example #1
0
        //public virtual convertToUnit()

        /// <summary>
        /// Gets the optimal unit.
        /// </summary>
        /// <param name="current">The current.</param>
        /// <param name="length">The length.</param>
        /// <returns></returns>
        public measureSystemUnitEntry GetOptimalUnit(measureSystemUnitEntry current, Int32 length)
        {
            measureSystemUnitEntry candidate = current;
            measureSystemUnitEntry next      = null;
            double factorDistance            = 1;
            Int32  factorStrDistance         = 0;
            Int32  estLength  = length;
            Int32  difference = length - targetStringLength;

            if (length > targetStringLength)
            {
                while (estLength > targetStringLength)
                {
                    next = GetUnit(candidate, measureUnitLevelChange.goHigher);
                    if (candidate == next)
                    {
                        break;
                    }

                    factorStrDistance = next.GetFactorDistance(current).GetFactorStrLenChange();

                    estLength  = length - factorStrDistance;
                    difference = estLength - targetStringLength;
                    if (difference < factorStrDistance)
                    {
                        break;
                    }
                    candidate = next;
                }
            }
            else if (length < targetStringLength)
            {
                while (estLength < targetStringLength)
                {
                    next = GetUnit(candidate, measureUnitLevelChange.goLower);
                    if (candidate == next)
                    {
                        break;
                    }

                    factorStrDistance = next.GetFactorDistance(current).GetFactorStrLenChange();

                    estLength  = length + factorStrDistance;
                    difference = targetStringLength - estLength;
                    if (difference < factorStrDistance)
                    {
                        break;
                    }
                    candidate = next;
                }
            }

            return(candidate);
        }
        public override void convertToUnit(measureSystemUnitEntry targetUnit)
        {
            double fd = targetUnit.GetFactorDistance(info.unit); //.GetFactorDistance(targetUnit);

            primValue = primValue * Convert.ToDecimal(fd);
            info.unit = targetUnit;
        }
Example #3
0
        /// <summary>
        /// Gets the factor distance.
        /// </summary>
        /// <param name="current">The current.</param>
        /// <param name="direction">The direction.</param>
        /// <returns></returns>
        public double GetFactorDistance(measureSystemUnitEntry current, measureUnitLevelChange direction)
        {
            measureSystemUnitEntry next = GetUnit(current, direction);

            return(current.GetFactorDistance(next));
        }