public static string GetAbbreviation(BitRateUnit unit, [CanBeNull] string cultureName)
        {
            // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx
            IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName);

            return(UnitSystem.GetCached(provider).GetDefaultAbbreviation(unit));
        }
        public string ToString(BitRateUnit unit, [CanBeNull] IFormatProvider provider, int significantDigitsAfterRadix)
        {
            double value  = As(unit);
            string format = UnitFormatter.GetFormat(value, significantDigitsAfterRadix);

            return(ToString(unit, provider, format));
        }
        public string ToString(BitRateUnit unit, [CanBeNull] string cultureName, int significantDigitsAfterRadix)
        {
            double value  = As(unit);
            string format = UnitFormatter.GetFormat(value, significantDigitsAfterRadix);

            return(ToString(unit, cultureName, format));
        }
示例#4
0
        /// <summary>
        ///     Creates the quantity with the given numeric value and unit.
        /// </summary>
        /// <param name="value">The numeric value to construct this quantity with.</param>
        /// <param name="unit">The unit representation to construct this quantity with.</param>
        /// <remarks>Value parameter cannot be named 'value' due to constraint when targeting Windows Runtime Component.</remarks>
        /// <exception cref="ArgumentException">If value is NaN or Infinity.</exception>
        private BitRate(decimal value, BitRateUnit unit)
        {
            if (unit == BitRateUnit.Undefined)
            {
                throw new ArgumentException("The quantity can not be created with an undefined unit.", nameof(unit));
            }

            _value = value;
            _unit  = unit;
        }
示例#5
0
        /// <summary>
        ///     Convert to the unit representation <paramref name="unit" />.
        /// </summary>
        /// <returns>Value converted to the specified unit.</returns>
        public decimal As(BitRateUnit unit)
        {
            if (Unit == unit)
            {
                return(Convert.ToDecimal(Value));
            }

            var converted = AsBaseNumericType(unit);

            return(Convert.ToDecimal(converted));
        }
        /// <summary>
        ///     Convert to the unit representation <paramref name="unit" />.
        /// </summary>
        /// <returns>Value converted to the specified unit.</returns>
        public double As(BitRateUnit unit)
        {
            if (Unit == unit)
            {
                return(Convert.ToDouble(Value));
            }

            var converted = AsBaseNumericType(unit);

            return(Convert.ToDouble(converted));
        }
        /// <summary>
        ///     Parse a string with one or two quantities of the format "&lt;quantity&gt; &lt;unit&gt;".
        /// </summary>
        /// <param name="str">String to parse. Typically in the form: {number} {unit}</param>
        /// <param name="provider">Format to use when parsing number and unit. Defaults to <see cref="UnitSystem.DefaultCulture" />.</param>
        /// <example>
        ///     Length.Parse("5.5 m", new CultureInfo("en-US"));
        /// </example>
        /// <exception cref="ArgumentNullException">The value of 'str' cannot be null. </exception>
        /// <exception cref="ArgumentException">
        ///     Expected string to have one or two pairs of quantity and unit in the format
        ///     "&lt;quantity&gt; &lt;unit&gt;". Eg. "5.5 m" or "1ft 2in"
        /// </exception>
        /// <exception cref="AmbiguousUnitParseException">
        ///     More than one unit is represented by the specified unit abbreviation.
        ///     Example: Volume.Parse("1 cup") will throw, because it can refer to any of
        ///     <see cref="VolumeUnit.MetricCup" />, <see cref="VolumeUnit.UsLegalCup" /> and <see cref="VolumeUnit.UsCustomaryCup" />.
        /// </exception>
        /// <exception cref="UnitsNetException">
        ///     If anything else goes wrong, typically due to a bug or unhandled case.
        ///     We wrap exceptions in <see cref="UnitsNetException" /> to allow you to distinguish
        ///     Units.NET exceptions from other exceptions.
        /// </exception>
        public static BitRate Parse(string str, [CanBeNull] IFormatProvider provider)
        {
            if (str == null)
            {
                throw new ArgumentNullException(nameof(str));
            }

            provider = provider ?? UnitSystem.DefaultCulture;

            return(QuantityParser.Parse <BitRate, BitRateUnit>(str, provider,
                                                               delegate(string value, string unit, IFormatProvider formatProvider2)
            {
                double parsedValue = double.Parse(value, formatProvider2);
                BitRateUnit parsedUnit = ParseUnit(unit, formatProvider2);
                return From(parsedValue, parsedUnit);
            }, (x, y) => FromBitsPerSecond(x.BitsPerSecond + y.BitsPerSecond)));
        }
        public string ToString(BitRateUnit unit, [CanBeNull] IFormatProvider provider, [NotNull] string format, [NotNull] params object[] args)
        {
            if (format == null)
            {
                throw new ArgumentNullException(nameof(format));
            }
            if (args == null)
            {
                throw new ArgumentNullException(nameof(args));
            }

            provider = provider ?? UnitSystem.DefaultCulture;

            double value = As(unit);

            object[] formatArgs = UnitFormatter.GetFormatArgs(unit, value, provider, args);
            return(string.Format(provider, format, formatArgs));
        }
        /// <summary>
        ///     Parse a string with one or two quantities of the format "&lt;quantity&gt; &lt;unit&gt;".
        /// </summary>
        /// <param name="str">String to parse. Typically in the form: {number} {unit}</param>
        /// <param name="cultureName">Name of culture (ex: "en-US") to use when parsing number and unit. Defaults to <see cref="UnitSystem" />'s default culture.</param>
        /// <example>
        ///     Length.Parse("5.5 m", new CultureInfo("en-US"));
        /// </example>
        /// <exception cref="ArgumentNullException">The value of 'str' cannot be null. </exception>
        /// <exception cref="ArgumentException">
        ///     Expected string to have one or two pairs of quantity and unit in the format
        ///     "&lt;quantity&gt; &lt;unit&gt;". Eg. "5.5 m" or "1ft 2in"
        /// </exception>
        /// <exception cref="AmbiguousUnitParseException">
        ///     More than one unit is represented by the specified unit abbreviation.
        ///     Example: Volume.Parse("1 cup") will throw, because it can refer to any of
        ///     <see cref="VolumeUnit.MetricCup" />, <see cref="VolumeUnit.UsLegalCup" /> and <see cref="VolumeUnit.UsCustomaryCup" />.
        /// </exception>
        /// <exception cref="UnitsNetException">
        ///     If anything else goes wrong, typically due to a bug or unhandled case.
        ///     We wrap exceptions in <see cref="UnitsNetException" /> to allow you to distinguish
        ///     Units.NET exceptions from other exceptions.
        /// </exception>
        public static BitRate Parse(string str, [CanBeNull] string cultureName)
        {
            if (str == null)
            {
                throw new ArgumentNullException(nameof(str));
            }

            // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx
            IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName);

            return(QuantityParser.Parse <BitRate, BitRateUnit>(str, provider,
                                                               delegate(string value, string unit, IFormatProvider formatProvider2)
            {
                double parsedValue = double.Parse(value, formatProvider2);
                BitRateUnit parsedUnit = ParseUnit(unit, formatProvider2);
                return From(parsedValue, parsedUnit);
            }, (x, y) => FromBitsPerSecond(x.BitsPerSecond + y.BitsPerSecond)));
        }
        public string ToString(BitRateUnit unit, [CanBeNull] string cultureName, [NotNull] string format, [NotNull] params object[] args)
        {
            if (format == null)
            {
                throw new ArgumentNullException(nameof(format));
            }
            if (args == null)
            {
                throw new ArgumentNullException(nameof(args));
            }

            // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx
            IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName);

            double value = As(unit);

            object[] formatArgs = UnitFormatter.GetFormatArgs(unit, value, provider, args);
            return(string.Format(provider, format, formatArgs));
        }
示例#11
0
 public static string GetAbbreviation(BitRateUnit unit)
 {
     return(GetAbbreviation(unit, null));
 }
示例#12
0
        public static BitRate From(QuantityValue value, BitRateUnit fromUnit)
#endif
        {
            return(new BitRate((decimal)value, fromUnit));
        }
示例#13
0
 public static BitRate From(double value, BitRateUnit fromUnit)
示例#14
0
 BitRate(decimal numericValue, BitRateUnit unit)
 {
     _value = numericValue;
     _unit  = unit;
 }
示例#15
0
 /// <summary>
 ///     Creates the quantity with the given numeric value and unit.
 /// </summary>
 /// <param name="value">The numeric value to construct this quantity with.</param>
 /// <param name="unit">The unit representation to construct this quantity with.</param>
 /// <exception cref="ArgumentException">If value is NaN or Infinity.</exception>
 public BitRate(double value, BitRateUnit unit)
 {
     _value = value;
     _unit  = unit;
 }
        public static string GetAbbreviation(BitRateUnit unit, [CanBeNull] IFormatProvider provider)
        {
            provider = provider ?? UnitSystem.DefaultCulture;

            return(UnitSystem.GetCached(provider).GetDefaultAbbreviation(unit));
        }
示例#17
0
 public static bool TryParseUnit(string str, out BitRateUnit unit)
 {
     return(TryParseUnit(str, null, out unit));
 }
 /// <summary>
 ///     Get string representation of value and unit. Using two significant digits after radix.
 /// </summary>
 /// <param name="unit">Unit representation to use.</param>
 /// <param name="cultureName">Name of culture (ex: "en-US") to use for localization and number formatting. Defaults to <see cref="UnitSystem" />'s default culture.</param>
 /// <returns>String representation.</returns>
 public string ToString(BitRateUnit unit, [CanBeNull] string cultureName)
 {
     return(ToString(unit, cultureName, 2));
 }
示例#19
0
 public static BitRate From(decimal value, BitRateUnit fromUnit)
 {
     return(new BitRate((decimal)value, fromUnit));
 }
示例#20
0
        /// <summary>
        ///     Get unit abbreviation string.
        /// </summary>
        /// <param name="unit">Unit to get abbreviation for.</param>
        /// <returns>Unit abbreviation string.</returns>
        /// <param name="cultureName">Name of culture (ex: "en-US") to use when parsing number and unit. Defaults to <see cref="GlobalConfiguration.DefaultCulture" /> if null.</param>
        public static string GetAbbreviation(BitRateUnit unit, [CanBeNull] string cultureName)
        {
            IFormatProvider provider = GetFormatProviderFromCultureName(cultureName);

            return(UnitAbbreviationsCache.Default.GetDefaultAbbreviation(unit, provider));
        }
示例#21
0
        private decimal AsBaseNumericType(BitRateUnit unit)
        {
            if (Unit == unit)
            {
                return(_value);
            }

            var baseUnitValue = AsBaseUnit();

            switch (unit)
            {
            case BitRateUnit.BitPerSecond: return(baseUnitValue);

            case BitRateUnit.BytePerSecond: return(baseUnitValue / 8m);

            case BitRateUnit.ExabitPerSecond: return((baseUnitValue) / 1e18m);

            case BitRateUnit.ExabytePerSecond: return((baseUnitValue / 8m) / 1e18m);

            case BitRateUnit.ExbibitPerSecond: return((baseUnitValue) / (1024m * 1024 * 1024 * 1024 * 1024 * 1024));

            case BitRateUnit.ExbibytePerSecond: return((baseUnitValue / 8m) / (1024m * 1024 * 1024 * 1024 * 1024 * 1024));

            case BitRateUnit.GibibitPerSecond: return((baseUnitValue) / (1024m * 1024 * 1024));

            case BitRateUnit.GibibytePerSecond: return((baseUnitValue / 8m) / (1024m * 1024 * 1024));

            case BitRateUnit.GigabitPerSecond: return((baseUnitValue) / 1e9m);

            case BitRateUnit.GigabytePerSecond: return((baseUnitValue / 8m) / 1e9m);

            case BitRateUnit.KibibitPerSecond: return((baseUnitValue) / 1024m);

            case BitRateUnit.KibibytePerSecond: return((baseUnitValue / 8m) / 1024m);

            case BitRateUnit.KilobitPerSecond: return((baseUnitValue) / 1e3m);

            case BitRateUnit.KilobytePerSecond: return((baseUnitValue / 8m) / 1e3m);

            case BitRateUnit.MebibitPerSecond: return((baseUnitValue) / (1024m * 1024));

            case BitRateUnit.MebibytePerSecond: return((baseUnitValue / 8m) / (1024m * 1024));

            case BitRateUnit.MegabitPerSecond: return((baseUnitValue) / 1e6m);

            case BitRateUnit.MegabytePerSecond: return((baseUnitValue / 8m) / 1e6m);

            case BitRateUnit.PebibitPerSecond: return((baseUnitValue) / (1024m * 1024 * 1024 * 1024 * 1024));

            case BitRateUnit.PebibytePerSecond: return((baseUnitValue / 8m) / (1024m * 1024 * 1024 * 1024 * 1024));

            case BitRateUnit.PetabitPerSecond: return((baseUnitValue) / 1e15m);

            case BitRateUnit.PetabytePerSecond: return((baseUnitValue / 8m) / 1e15m);

            case BitRateUnit.TebibitPerSecond: return((baseUnitValue) / (1024m * 1024 * 1024 * 1024));

            case BitRateUnit.TebibytePerSecond: return((baseUnitValue / 8m) / (1024m * 1024 * 1024 * 1024));

            case BitRateUnit.TerabitPerSecond: return((baseUnitValue) / 1e12m);

            case BitRateUnit.TerabytePerSecond: return((baseUnitValue / 8m) / 1e12m);

            default:
                throw new NotImplementedException($"Can not convert {Unit} to {unit}.");
            }
        }
示例#22
0
 /// <summary>
 ///     Convert to the unit representation <paramref name="unit" />.
 /// </summary>
 /// <returns>Value converted to the specified unit.</returns>
 public double As(BitRateUnit unit) => GetValueAs(unit);
示例#23
0
 /// <summary>
 ///     Get string representation of value and unit. Using current UI culture and two significant digits after radix.
 /// </summary>
 /// <param name="unit">Unit representation to use.</param>
 /// <returns>String representation.</returns>
 public string ToString(BitRateUnit unit)
 {
     return(ToString(unit, null, 2));
 }
示例#24
0
        public static string GetAbbreviation(
            BitRateUnit unit,
#if WINDOWS_UWP
            [CanBeNull] string cultureName)
 /// <summary>
 ///     Dynamically convert from value and unit enum <see cref="BitRateUnit" /> to <see cref="BitRate" />.
 /// </summary>
 /// <param name="value">Value to convert from.</param>
 /// <param name="fromUnit">Unit to convert from.</param>
 /// <returns>BitRate unit value.</returns>
 public static BitRate?From(QuantityValue?value, BitRateUnit fromUnit)
 {
     return(value.HasValue ? new BitRate((decimal)value.Value, fromUnit) : default(BitRate?));
 }
示例#26
0
        /// <summary>
        ///     Parse a unit string.
        /// </summary>
        /// <param name="str">String to parse. Typically in the form: {number} {unit}</param>
        /// <param name="unit">The parsed unit if successful.</param>
        /// <returns>True if successful, otherwise false.</returns>
        /// <example>
        ///     Length.TryParseUnit("m", new CultureInfo("en-US"));
        /// </example>
        /// <param name="cultureName">Name of culture (ex: "en-US") to use when parsing number and unit. Defaults to <see cref="GlobalConfiguration.DefaultCulture" /> if null.</param>
        public static bool TryParseUnit(string str, [CanBeNull] string cultureName, out BitRateUnit unit)
        {
            IFormatProvider provider = GetFormatProviderFromCultureName(cultureName);

            return(UnitParser.Default.TryParse <BitRateUnit>(str, provider, out unit));
        }
 /// <summary>
 ///     Get string representation of value and unit. Using two significant digits after radix.
 /// </summary>
 /// <param name="unit">Unit representation to use.</param>
 /// <param name="provider">Format to use for localization and number formatting. Defaults to <see cref="UnitSystem.DefaultCulture" />.</param>
 /// <returns>String representation.</returns>
 public string ToString(BitRateUnit unit, [CanBeNull] IFormatProvider provider)
 {
     return(ToString(unit, provider, 2));
 }
示例#28
0
 /// <summary>
 ///     Dynamically convert from value and unit enum <see cref="BitRateUnit" /> to <see cref="BitRate" />.
 /// </summary>
 /// <param name="value">Value to convert from.</param>
 /// <param name="fromUnit">Unit to convert from.</param>
 /// <returns>BitRate unit value.</returns>
 public static BitRate From(double value, BitRateUnit fromUnit)
 {
     return(new BitRate(value, fromUnit));
 }
示例#29
0
        /// <summary>
        ///     Converts this BitRate to another BitRate with the unit representation <paramref name="unit" />.
        /// </summary>
        /// <returns>A BitRate with the specified unit.</returns>
        public BitRate ToUnit(BitRateUnit unit)
        {
            var convertedValue = AsBaseNumericType(unit);

            return(new BitRate(convertedValue, unit));
        }
示例#30
0
        /// <summary>
        ///     Converts this Duration to another Duration with the unit representation <paramref name="unit" />.
        /// </summary>
        /// <returns>A Duration with the specified unit.</returns>
        public BitRate ToUnit(BitRateUnit unit)
        {
            var convertedValue = GetValueAs(unit);

            return(new BitRate(convertedValue, unit));
        }