示例#1
0
        /// <summary>
        /// Convert distance in metric unit to imperial / us unit
        /// </summary>
        /// <typeparam name="T">distance type</typeparam>
        /// <param name="distance">numeric distance</param>
        /// <param name="from">Metric unit</param>
        /// <param name="to">Imperial / us unit</param>
        /// <param name="decimals">number of decimals in output </param>
        /// <returns>The converted double imperial value</returns>
        public static double ConvertDistance <T>(this T distance, DistanceUnitsMetrics from,
                                                 DistanceUnitsImperialUS to, int?decimals = null) where T :
        struct,
        IComparable,
        IComparable <T>,
        IConvertible,
        IEquatable <T>,
        IFormattable
        {
            var dd = (double)Convert.ChangeType(distance, typeof(double));

            if (from != DistanceUnitsMetrics.Meter)
            {
                dd = dd.ConvertDistance(from, DistanceUnitsMetrics.Meter);
            }

            var imperial = dd / ImperialUsInMeters[to.ToString()];

            return(decimals != null?Math.Round(imperial, (int)decimals) : imperial);
        }
示例#2
0
        /// <summary>
        /// Convert Imperial / us unit to metric unit
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="distance">Distance to convert</param>
        /// <param name="from">Current unit</param>
        /// <param name="to">Unit to convert to</param>
        /// <param name="decimals">number of decimals in output</param>
        /// <returns>Converted distance</returns>
        public static double ConvertDistance <T>(this T distance, DistanceUnitsImperialUS from,
                                                 DistanceUnitsMetrics to, int?decimals = null) where T :
        struct,
        IComparable,
        IComparable <T>,
        IConvertible,
        IEquatable <T>,
        IFormattable
        {
            var dd = (double)Convert.ChangeType(distance, typeof(double));

            var meters = dd * ImperialUsInMeters[from.ToString()];

            if (to == DistanceUnitsMetrics.Meter)
            {
                return(decimals != null?Math.Round(meters, (int)decimals) : meters);
            }

            var metric = meters.ConvertDistance(DistanceUnitsMetrics.Meter, to);

            return(decimals != null?Math.Round(metric, (int)decimals) : metric);
        }
示例#3
0
        /// <summary>
        /// Convert numeric type value in metric to double metric value.
        /// </summary>
        /// <typeparam name="T">Numeric type</typeparam>
        /// <param name="distance">Distance in metric unit to convert to metric unit</param>
        /// <param name="from">Current metric unit</param>
        /// <param name="to">transform to metric unit</param>
        /// <param name="decimals">number of decimal points</param>
        /// <returns>return transformed metric value as double</returns>
        public static double ConvertDistance <T>(this T distance, DistanceUnitsMetrics from, DistanceUnitsMetrics to, int?decimals = null) where T :
        struct,
        IComparable,
        IComparable <T>,
        IConvertible,
        IEquatable <T>,
        IFormattable
        {
            int    pow;
            var    dd = (double)Convert.ChangeType(distance, typeof(double));
            double result;

            if ((int)to == (int)from)
            {
                return(dd);
            }

            if ((int)to < (int)from)
            {
                pow    = (int)to - (int)from;
                result = dd * Math.Pow(10, Math.Abs(pow));

                return(decimals != null?Math.Round(result, (int)decimals) : result);
            }

            if (from >= 0)
            {
                pow = ((int)to + (int)from) * -1;
            }
            else
            {
                pow = (int)to + (int)from;
            }

            result = dd * Math.Pow(10, pow);

            return(decimals != null?Math.Round(result, (int)decimals) : result);
        }
示例#4
0
 public Distance <T> From(DistanceUnitsMetrics from)
 {
     _fromUnit = from;
     return(this);
 }
示例#5
0
 public Distance <T> To(DistanceUnitsMetrics to)
 {
     _toUnit = to;
     return(this);
 }