示例#1
0
 /// <summary>
 /// Initialize the category and source/target units
 /// </summary>
 /// <param name="category">unit category</param>
 /// <param name="sourceUnit">Source Unit </param>
 /// <param name="targetUnit">Target unit</param>
 public CurrentConversion(
     CategoryInformation category,
     UnitInformation sourceUnit,
     UnitInformation targetUnit)
 {
     this.UserInput         = new StringBuilder();
     this.IsUpperUnitSource = true;
     this.CurrentCategory   = category;
     this.SourceUnit        = sourceUnit;
     this.TargetUnit        = targetUnit;
 }
 /// <summary>
 /// Initialize the category and source/target units
 /// </summary>
 /// <param name="category">unit category</param>
 /// <param name="sourceUnit">Source Unit </param>
 /// <param name="targetUnit">Target unit</param>
 public CurrentConversion(
     CategoryInformation category , 
     UnitInformation sourceUnit ,
     UnitInformation targetUnit )
 {
     this.UserInput = new StringBuilder();
     this.IsUpperUnitSource = true;
     this.CurrentCategory = category;
     this.SourceUnit = sourceUnit;
     this.TargetUnit = targetUnit;
 }
        /// <summary>
        /// Converts the specified value from the source unit type, to the target
        /// unit value
        /// </summary>
        /// <param name="valueToConvert">The value to convert.</param>
        /// <param name="source">The source unit</param>
        /// <param name="target">The target unit</param>
        /// <returns></returns>
        internal static double Convert(
            double valueToConvert,
            UnitInformation source,
            UnitInformation target)
        {
            if ((source == null) || (target == null))
            {
                throw new ArgumentNullException("source", "source or dest was null");
            }

            if (source == target)
            {
                return(valueToConvert);
            }
            double result = 0.0;

            // Convert to the base unit type for the category.
            if (source.FormulaInvert)
            {
                result = (valueToConvert - source.Offset) * source.Multiplier;
            }
            else
            {
                result = valueToConvert * source.Multiplier + source.Offset;
            }

            // Convert to the target unit
            if (target.FormulaInvert)
            {
                result = result / target.Multiplier + target.Offset;
            }
            else
            {
                result = (result - target.Offset) / target.Multiplier;
            }
            return(result);
        }
        /// <summary>
        /// Converts the specified value from the source unit type, to the target 
        /// unit value
        /// </summary>
        /// <param name="valueToConvert">The value to convert.</param>
        /// <param name="source">The source unit</param>
        /// <param name="target">The target unit</param>
        /// <returns></returns>
        internal static double Convert(
            double valueToConvert , 
            UnitInformation source, 
            UnitInformation target)
        {

            if ((source == null) || (target == null))
            {
                throw new ArgumentNullException("source" , "source or dest was null" );
            }

            if (source == target)
            {
                return valueToConvert;
            }
            double result = 0.0;
            // Convert to the base unit type for the category. 
            if (source.FormulaInvert)
            {
                result = (valueToConvert - source.Offset) * source.Multiplier;
            }
            else
            {
                result = valueToConvert * source.Multiplier + source.Offset;
            }

            // Convert to the target unit
            if (target.FormulaInvert)
            {
                result = result / target.Multiplier + target.Offset;
            }
            else
            {
                result = (result - target.Offset) / target.Multiplier;
            }
            return result;
        }