Esempio n. 1
0
        /// <summary>
        /// Conversion from basic unit quantity to a multiple or fraction
        /// </summary>
        /// <param name="quantity">Basic unit quantity</param>
        /// <param name="prefix">Metric prefix corresponding to desired multiple or fraction</param>
        public static double FromBase(double quantity, MetricPrefix prefix)
        {
            switch (prefix)
            {
            case MetricPrefix.None: return(quantity);

            case MetricPrefix.Atto: return(quantity * 1e18);

            case MetricPrefix.Femto: return(quantity * 1e15);

            case MetricPrefix.Piko: return(quantity * 1e12);

            case MetricPrefix.Nano: return(quantity * 1e9);

            case MetricPrefix.Mikro: return(quantity * 1e6);

            case MetricPrefix.Milli: return(quantity * 1e3);

            case MetricPrefix.Kilo: return(quantity * 1e-3);

            case MetricPrefix.Mega: return(quantity * 1e-6);

            case MetricPrefix.Giga: return(quantity * 1e-9);

            case MetricPrefix.Tera: return(quantity * 1e-12);

            case MetricPrefix.Peta: return(quantity * 1e-15);

            case MetricPrefix.Exa: return(quantity * 1e-18);

            default: return(quantity);
            }
        }
        /// <summary>
        /// Build a Metric representation of the number.
        /// </summary>
        /// <param name="input">Number to convert to a Metric representation.</param>
        /// <param name="hasSpace">True will split the number and the symbol with a whitespace.</param>
        /// <param name="prefixType">Type of prefix to append</param>
        /// <param name="decimals">If not null it is the numbers of decimals to round the number to</param>
        /// <returns>A number in a Metric representation</returns>
        private static string BuildRepresentation(double input, bool hasSpace, MetricPrefix prefixType, int?decimals)
        {
            var exponent = (int)Math.Floor(Math.Log10(Math.Abs(input)) / 3);

            return(exponent.Equals(0)
                ? input.ToString()
                : BuildMetricRepresentation(input, exponent, hasSpace, prefixType, decimals));
        }
Esempio n. 3
0
    private static void ListMetricPrefixes()
    {
        Console.ForegroundColor = HelpColor;

        int[] exo = { 24, 21, 18, 15, 12, 9, 6, 3, 2, 1, 0, -1, -2, -3, -6, -9, -12, -15, -18, -21, -24 };
        foreach (int e in exo)
        {
            MetricPrefix pr = MetricPrefix.FromExponent(e);
            Console.WriteLine("     {0}         {1}        10^{2}", pr.Prefix.PadRight(10), pr.Symbol.PadRight(10), pr.Exponent);
        }

        Console.ForegroundColor = ForegroundColor;
    }
        /// <summary>
        /// Converts a number into a valid and Human-readable Metric representation.
        /// </summary>
        /// <remarks>
        /// Inspired by a snippet from Thom Smith.
        /// See <a href="http://stackoverflow.com/questions/12181024/formatting-a-number-with-a-metric-prefix">this link</a> for more.
        /// </remarks>
        /// <param name="input">Number to convert to a Metric representation.</param>
        /// <param name="hasSpace">True will split the number and the symbol with a whitespace.</param>
        /// <param name="prefixType">Type of prefix to append</param>
        /// <param name="decimals">If not null it is the numbers of decimals to round the number to</param>
        /// <example>
        /// <code>
        /// 1000d.ToMetric() => "1k"
        /// 123d.ToMetric() => "123"
        /// 1E-1.ToMetric() => "100m"
        /// </code>
        /// </example>
        /// <returns>A valid Metric representation</returns>
        public static string ToMetric(this double input, bool hasSpace = false, MetricPrefix prefixType = MetricPrefix.Symbol, int?decimals = null)
        {
            if (input.Equals(0))
            {
                return(input.ToString());
            }

            if (input.IsOutOfRange())
            {
                throw new ArgumentOutOfRangeException(nameof(input));
            }

            return(BuildRepresentation(input, hasSpace, prefixType, decimals));
        }
        /// <summary>
        /// Get the unit from a symbol
        /// </summary>
        /// <param name="symbol">The symbol linked to the unit</param>
        /// <param name="prefixType">Enum of prefix type to be returned</param>
        /// <returns>String of the prefix</returns>
        private static string GetUnit(char symbol, MetricPrefix prefixType)
        {
            switch (prefixType)
            {
            case MetricPrefix.Long:
                return(LongScaleWords[symbol]);

            case MetricPrefix.Short:
                return(ShortScaleWords[symbol]);

            case MetricPrefix.Word:
                return(Names[symbol]);

            default:
                return(symbol.ToString());
            }
        }
Esempio n. 6
0
            public UnitInfo(MetricPrefix prefix, UnitType baseUnit)
            {
                Double mult = Math.Pow(10.0, -(Double)(Int32)prefix);

                if (!prefixLookup.TryGetValue(prefix, out var unitPref))
                {
                    unitPref = "?";
                }

                if (!unitNameLookup.TryGetValue(baseUnit, out var unitName))
                {
                    unitName = "?";
                }
                if (!unitConversionLookup.TryGetValue(baseUnit, out var conversion))
                {
                    conversion = 1.0f;
                }
                this.conversionFromMeters = conversion * mult;
                this.name = String.Format("{0}{1}", prefixLookup[prefix], unitName);
            }
Esempio n. 7
0
 /// <summary>
 /// Sets new quantity
 /// </summary>
 /// <param name="quantity">Quantity</param>
 /// <param name="prefix">Metric prefix corresponding to specified quantity</param>
 public void Set(double quantity, MetricPrefix prefix = MetricPrefix.None)
 {
     _quantity = ToBase(quantity, prefix);
     return;
 }
Esempio n. 8
0
 //Methods
 /// <summary>
 /// Returns multiple or fraction of the quantity
 /// </summary>
 /// <param name="prefix">Metric prefix corresponding to desired multiple or fraction of the basic unit</param>
 public double Get(MetricPrefix prefix = MetricPrefix.None)
 {
     return(FromBase(_quantity, prefix));
 }
Esempio n. 9
0
 /// <summary>
 /// Instantiates an initialized instance
 /// </summary>
 /// <param name="quantity">Quantity</param>
 /// <param name="prefix">Metric prefix corresponding to specified quantity</param>
 /// <param name="unit">Unit of specified quantity</param>
 public Quantity(double quantity, MetricPrefix prefix, SIUnit unit)
 {
     _unit     = unit;
     _quantity = ToBase(quantity, prefix);
     return;
 }
Esempio n. 10
0
 public void ToMetric(string expected, double input, bool hasSpace, MetricPrefix prefixType, int?decimals)
 {
     Assert.Equal(expected, input.ToMetric(hasSpace, prefixType, decimals));
 }
Esempio n. 11
0
 public static float GetFactor(this MetricPrefix prefix) =>
 MathF.Pow(10f, prefix.GetFactorValue());
Esempio n. 12
0
 private static sbyte GetFactorValue(this MetricPrefix prefix) =>
 typeof(MetricPrefix)
 .GetMember($"{prefix}")
 .First()
 .GetCustomAttribute <MetricMetadataAttribute>()
 ._factor;
Esempio n. 13
0
 public static char GetSymbol(this MetricPrefix prefix) =>
 typeof(MetricPrefix)
 .GetMember($"{prefix}")
 .First()
 .GetCustomAttribute <MetricMetadataAttribute>()
 ._symbol;
Esempio n. 14
0
 /// <summary>
 /// Convenience constructor for initializing prefixed non-standard unit
 /// </summary>
 /// <param name="prefix">Prefix to use in unit naming and scaling vis-a-vis standard unit</param>
 public ConstantConverterUnit(MetricPrefix prefix)
     : this($"{prefix.GetSymbol()}{new Q().StandardUnit.Symbol}", prefix.GetFactor())
 {
 }
        /// <summary>
        /// Build a Metric representation of the number.
        /// </summary>
        /// <param name="input">Number to convert to a Metric representation.</param>
        /// <param name="exponent">Exponent of the number in a scientific notation</param>
        /// <param name="hasSpace">True will split the number and the symbol with a whitespace.</param>
        /// <param name="prefixType">Type of prefix to append</param>
        /// <param name="decimals">If not null it is the numbers of decimals to round the number to</param>
        /// <returns>A number in a Metric representation</returns>
        private static string BuildMetricRepresentation(double input, int exponent, bool hasSpace, MetricPrefix prefixType, int?decimals)
        {
            var number = input * Math.Pow(1000, -exponent);

            if (decimals.HasValue)
            {
                number = Math.Round(number, decimals.Value);
            }

            var symbol = Math.Sign(exponent) == 1
                ? Symbols[0][exponent - 1]
                : Symbols[1][-exponent - 1];

            return(number
                   + (hasSpace ? " " : string.Empty)
                   + GetUnit(symbol, prefixType));
        }
 /// <summary>
 /// Converts a number into a valid and Human-readable Metric representation.
 /// </summary>
 /// <remarks>
 /// Inspired by a snippet from Thom Smith.
 /// See <a href="http://stackoverflow.com/questions/12181024/formatting-a-number-with-a-metric-prefix">this link</a> for more.
 /// </remarks>
 /// <param name="input">Number to convert to a Metric representation.</param>
 /// <param name="hasSpace">True will split the number and the symbol with a whitespace.</param>
 /// <param name="prefixType">Type of prefix to append</param>
 /// <param name="decimals">If not null it is the numbers of decimals to round the number to</param>
 /// <example>
 /// <code>
 /// 1000.ToMetric() => "1k"
 /// 123.ToMetric() => "123"
 /// 1E-1.ToMetric() => "100m"
 /// </code>
 /// </example>
 /// <returns>A valid Metric representation</returns>
 public static string ToMetric(this int input, bool hasSpace = false, MetricPrefix prefixType = MetricPrefix.Symbol, int?
                               decimals = null)
 {
     return(((double)input).ToMetric(hasSpace, prefixType, decimals));
 }