public string ToString(ReactivePowerUnit unit, [CanBeNull] string cultureName, int significantDigitsAfterRadix) { double value = As(unit); string format = UnitFormatter.GetFormat(value, significantDigitsAfterRadix); return(ToString(unit, cultureName, format)); }
public static string GetAbbreviation(ReactivePowerUnit 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(ReactivePowerUnit unit, [CanBeNull] IFormatProvider provider, int significantDigitsAfterRadix) { double value = As(unit); string format = UnitFormatter.GetFormat(value, significantDigitsAfterRadix); return(ToString(unit, provider, format)); }
// Windows Runtime Component does not support nullable types (double?): https://msdn.microsoft.com/en-us/library/br230301.aspx #if !WINDOWS_UWP /// <summary> /// Dynamically convert from value and unit enum <see cref="ReactivePowerUnit" /> to <see cref="ReactivePower" />. /// </summary> /// <param name="value">Value to convert from.</param> /// <param name="fromUnit">Unit to convert from.</param> /// <returns>ReactivePower unit value.</returns> public static ReactivePower?From(QuantityValue?value, ReactivePowerUnit fromUnit) { if (!value.HasValue) { return(null); } return(new ReactivePower((double)value.Value, fromUnit)); }
// ReSharper restore VirtualMemberNeverOverriden.Global protected (double UnitsInBaseUnit, double Tolerence) GetConversionFactor(ReactivePowerUnit unit) { return(unit switch { ReactivePowerUnit.GigavoltampereReactive => (GigavoltamperesReactiveInOneVoltampereReactive, GigavoltamperesReactiveTolerance), ReactivePowerUnit.KilovoltampereReactive => (KilovoltamperesReactiveInOneVoltampereReactive, KilovoltamperesReactiveTolerance), ReactivePowerUnit.MegavoltampereReactive => (MegavoltamperesReactiveInOneVoltampereReactive, MegavoltamperesReactiveTolerance), ReactivePowerUnit.VoltampereReactive => (VoltamperesReactiveInOneVoltampereReactive, VoltamperesReactiveTolerance), _ => throw new NotSupportedException() });
/// <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 ReactivePower(double value, ReactivePowerUnit unit) { if (unit == ReactivePowerUnit.Undefined) { throw new ArgumentException("The quantity can not be created with an undefined unit.", nameof(unit)); } _value = value; _unit = unit; }
/// <summary> /// Creates the quantity with the given numeric value and unit. /// </summary> /// <param name="numericValue">The numeric value to contruct this quantity with.</param> /// <param name="unit">The unit representation to contruct 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 ReactivePower(double numericValue, ReactivePowerUnit unit) { if (unit == ReactivePowerUnit.Undefined) { throw new ArgumentException("The quantity can not be created with an undefined unit.", nameof(unit)); } _value = Guard.EnsureValidNumber(numericValue, nameof(numericValue)); _unit = unit; }
private double AsBaseNumericType(ReactivePowerUnit unit) { if (Unit == unit) { return(_value); } var baseUnitValue = AsBaseUnit(); switch (unit) { case ReactivePowerUnit.CentivoltampereReactive: return((baseUnitValue) / 1e-2d); case ReactivePowerUnit.DecavoltampereReactive: return((baseUnitValue) / 1e1d); case ReactivePowerUnit.DecivoltampereReactive: return((baseUnitValue) / 1e-1d); case ReactivePowerUnit.ExavoltampereReactive: return((baseUnitValue) / 1e18d); case ReactivePowerUnit.FemtovoltampereReactive: return((baseUnitValue) / 1e-15d); case ReactivePowerUnit.GigavoltampereReactive: return((baseUnitValue) / 1e9d); case ReactivePowerUnit.HectovoltampereReactive: return((baseUnitValue) / 1e2d); case ReactivePowerUnit.KilovoltampereReactive: return((baseUnitValue) / 1e3d); case ReactivePowerUnit.MegavoltampereReactive: return((baseUnitValue) / 1e6d); case ReactivePowerUnit.MicrovoltampereReactive: return((baseUnitValue) / 1e-6d); case ReactivePowerUnit.MillivoltampereReactive: return((baseUnitValue) / 1e-3d); case ReactivePowerUnit.NanovoltampereReactive: return((baseUnitValue) / 1e-9d); case ReactivePowerUnit.PetavoltampereReactive: return((baseUnitValue) / 1e15d); case ReactivePowerUnit.PicovoltampereReactive: return((baseUnitValue) / 1e-12d); case ReactivePowerUnit.QutravoltampereReactive: return((baseUnitValue) / 1e27d); case ReactivePowerUnit.TeravoltampereReactive: return((baseUnitValue) / 1e12d); case ReactivePowerUnit.VettavoltampereReactive: return((baseUnitValue) / 1e30d); case ReactivePowerUnit.VoltampereReactive: return(baseUnitValue); case ReactivePowerUnit.YottavoltampereReactive: return((baseUnitValue) / 1e24d); case ReactivePowerUnit.ZettavoltampereReactive: return((baseUnitValue) / 1e21d); default: throw new NotImplementedException($"Can not convert {Unit} to {unit}."); } }
/// <summary> /// Convert to the unit representation <paramref name="unit" />. /// </summary> /// <returns>Value converted to the specified unit.</returns> public double As(ReactivePowerUnit unit) { if (Unit == unit) { return(Convert.ToDouble(Value)); } var converted = AsBaseNumericType(unit); return(Convert.ToDouble(converted)); }
public string ToString(ReactivePowerUnit 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); }
/// <summary> /// Parse a string with one or two quantities of the format "<quantity> <unit>". /// </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 /// "<quantity> <unit>". 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 ReactivePower 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<ReactivePower, ReactivePowerUnit>(str, provider, delegate(string value, string unit, IFormatProvider formatProvider2) { double parsedValue = double.Parse(value, formatProvider2); ReactivePowerUnit parsedUnit = ParseUnit(unit, formatProvider2); return From(parsedValue, parsedUnit); }, (x, y) => FromVoltamperesReactive(x.VoltamperesReactive + y.VoltamperesReactive)); }
/// <summary> /// Parse a string with one or two quantities of the format "<quantity> <unit>". /// </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 /// "<quantity> <unit>". 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 ReactivePower Parse(string str, [CanBeNull] IFormatProvider provider) { if (str == null) { throw new ArgumentNullException(nameof(str)); } provider = provider ?? UnitSystem.DefaultCulture; return(QuantityParser.Parse <ReactivePower, ReactivePowerUnit>(str, provider, delegate(string value, string unit, IFormatProvider formatProvider2) { double parsedValue = double.Parse(value, formatProvider2); ReactivePowerUnit parsedUnit = ParseUnit(unit, formatProvider2); return From(parsedValue, parsedUnit); }, (x, y) => FromVoltamperesReactive(x.VoltamperesReactive + y.VoltamperesReactive))); }
/// <summary> /// Convert to the unit representation <paramref name="unit" />. /// </summary> /// <returns>Value in new unit if successful, exception otherwise.</returns> /// <exception cref="NotImplementedException">If conversion was not successful.</exception> public double As(ReactivePowerUnit unit) { switch (unit) { case ReactivePowerUnit.KilovoltampereReactive: return(KilovoltamperesReactive); case ReactivePowerUnit.MegavoltampereReactive: return(MegavoltamperesReactive); case ReactivePowerUnit.VoltampereReactive: return(VoltamperesReactive); default: throw new NotImplementedException("unit: " + unit); } }
/// <summary> /// Dynamically convert from value and unit enum <see cref="ReactivePowerUnit" /> to <see cref="ReactivePower" />. /// </summary> /// <param name="val">Value to convert from.</param> /// <param name="fromUnit">Unit to convert from.</param> /// <returns>ReactivePower unit value.</returns> public static ReactivePower From(double val, ReactivePowerUnit fromUnit) { switch (fromUnit) { case ReactivePowerUnit.KilovoltampereReactive: return(FromKilovoltamperesReactive(val)); case ReactivePowerUnit.MegavoltampereReactive: return(FromMegavoltamperesReactive(val)); case ReactivePowerUnit.VoltampereReactive: return(FromVoltamperesReactive(val)); default: throw new NotImplementedException("fromUnit: " + fromUnit); } }
public string ToString(ReactivePowerUnit 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 "<quantity> <unit>". /// </summary> /// <param name="str">String to parse. Typically in the form: {number} {unit}</param> /// <param name="culture">Format to use when parsing number and unit. If it is null, it defaults to <see cref="NumberFormatInfo.CurrentInfo"/> for parsing the number and <see cref="CultureInfo.CurrentUICulture"/> for parsing the unit abbreviation by culture/language.</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 /// "<quantity> <unit>". 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 ReactivePower Parse(string str, [CanBeNull] Culture culture) { if (str == null) { throw new ArgumentNullException("str"); } #if WINDOWS_UWP IFormatProvider formatProvider = culture == null ? null : new CultureInfo(culture); #else IFormatProvider formatProvider = culture; #endif return(UnitParser.ParseUnit <ReactivePower>(str, formatProvider, delegate(string value, string unit, IFormatProvider formatProvider2) { double parsedValue = double.Parse(value, formatProvider2); ReactivePowerUnit parsedUnit = ParseUnit(unit, formatProvider2); return From(parsedValue, parsedUnit); }, (x, y) => FromVoltamperesReactive(x.VoltamperesReactive + y.VoltamperesReactive))); }
public static ReactivePower From(QuantityValue value, ReactivePowerUnit fromUnit) #endif { switch (fromUnit) { case ReactivePowerUnit.GigavoltampereReactive: return(FromGigavoltamperesReactive(value)); case ReactivePowerUnit.KilovoltampereReactive: return(FromKilovoltamperesReactive(value)); case ReactivePowerUnit.MegavoltampereReactive: return(FromMegavoltamperesReactive(value)); case ReactivePowerUnit.VoltampereReactive: return(FromVoltamperesReactive(value)); default: throw new NotImplementedException("fromUnit: " + fromUnit); } }
/// <summary> /// Parse a string with one or two quantities of the format "<quantity> <unit>". /// </summary> /// <param name="str">String to parse. Typically in the form: {number} {unit}</param> /// <param name="culture">Format to use when parsing number and unit. If it is null, it defaults to <see cref="NumberFormatInfo.CurrentInfo"/> for parsing the number and <see cref="CultureInfo.CurrentUICulture"/> for parsing the unit abbreviation by culture/language.</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 /// "<quantity> <unit>". 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 ReactivePower Parse(string str, [CanBeNull] Culture culture) { if (str == null) { throw new ArgumentNullException("str"); } // Windows Runtime Component does not support CultureInfo type, so use culture name string for public methods instead: https://msdn.microsoft.com/en-us/library/br230301.aspx #if WINDOWS_UWP IFormatProvider formatProvider = culture == null ? null : new CultureInfo(culture); #else IFormatProvider formatProvider = culture; #endif return(QuantityParser.Parse <ReactivePower, ReactivePowerUnit>(str, formatProvider, delegate(string value, string unit, IFormatProvider formatProvider2) { double parsedValue = double.Parse(value, formatProvider2); ReactivePowerUnit parsedUnit = ParseUnit(unit, formatProvider2); return From(parsedValue, parsedUnit); }, (x, y) => FromVoltamperesReactive(x.VoltamperesReactive + y.VoltamperesReactive))); }
// Windows Runtime Component does not support nullable types (double?): https://msdn.microsoft.com/en-us/library/br230301.aspx #if !WINDOWS_UWP /// <summary> /// Dynamically convert from value and unit enum <see cref="ReactivePowerUnit" /> to <see cref="ReactivePower" />. /// </summary> /// <param name="value">Value to convert from.</param> /// <param name="fromUnit">Unit to convert from.</param> /// <returns>ReactivePower unit value.</returns> public static ReactivePower?From(double?value, ReactivePowerUnit fromUnit) { if (!value.HasValue) { return(null); } switch (fromUnit) { case ReactivePowerUnit.KilovoltampereReactive: return(FromKilovoltamperesReactive(value.Value)); case ReactivePowerUnit.MegavoltampereReactive: return(FromMegavoltamperesReactive(value.Value)); case ReactivePowerUnit.VoltampereReactive: return(FromVoltamperesReactive(value.Value)); default: throw new NotImplementedException("fromUnit: " + fromUnit); } }
public string ToString(ReactivePowerUnit unit, [CanBeNull] Culture culture, [NotNull] string format, [NotNull] params object[] args) { if (format == null) { throw new ArgumentNullException(nameof(format)); } if (args == null) { throw new ArgumentNullException(nameof(args)); } #if WINDOWS_UWP IFormatProvider formatProvider = culture == null ? null : new CultureInfo(culture); #else IFormatProvider formatProvider = culture; #endif double value = As(unit); object[] formatArgs = UnitFormatter.GetFormatArgs(unit, value, formatProvider, args); return(string.Format(formatProvider, format, formatArgs)); }
public string ToString(ReactivePowerUnit unit, [CanBeNull] Culture culture, [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 type, so use culture name string for public methods instead: https://msdn.microsoft.com/en-us/library/br230301.aspx #if WINDOWS_UWP IFormatProvider formatProvider = culture == null ? null : new CultureInfo(culture); #else IFormatProvider formatProvider = culture; #endif double value = As(unit); object[] formatArgs = UnitFormatter.GetFormatArgs(unit, value, formatProvider, args); return(string.Format(formatProvider, format, formatArgs)); }
private double AsBaseNumericType(ReactivePowerUnit unit) { if (Unit == unit) { return(_value); } var baseUnitValue = AsBaseUnit(); switch (unit) { case ReactivePowerUnit.GigavoltampereReactive: return((baseUnitValue) / 1e9d); case ReactivePowerUnit.KilovoltampereReactive: return((baseUnitValue) / 1e3d); case ReactivePowerUnit.MegavoltampereReactive: return((baseUnitValue) / 1e6d); case ReactivePowerUnit.VoltampereReactive: return(baseUnitValue); default: throw new NotImplementedException($"Can not convert {Unit} to {unit}."); } }
public static string GetAbbreviation( ReactivePowerUnit unit, #if WINDOWS_UWP [CanBeNull] string cultureName)
public static string GetAbbreviation(ReactivePowerUnit unit) { return(GetAbbreviation(unit, null)); }
public static ReactivePower From(QuantityValue value, ReactivePowerUnit fromUnit) #endif { return(new ReactivePower((double)value, fromUnit)); }
public static ReactivePower From(double value, ReactivePowerUnit fromUnit)
ReactivePower(double numericValue, ReactivePowerUnit unit) { _value = numericValue; _unit = unit; }
/// <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(ReactivePowerUnit unit) { return(ToString(unit, null, 2)); }
/// <summary> /// Converts this ReactivePower to another ReactivePower with the unit representation <paramref name="unit" />. /// </summary> /// <returns>A ReactivePower with the specified unit.</returns> public ReactivePower ToUnit(ReactivePowerUnit unit) { var convertedValue = AsBaseNumericType(unit); return(new ReactivePower(convertedValue, unit)); }
/// <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 ReactivePowerUnit unit) { IFormatProvider provider = GetFormatProviderFromCultureName(cultureName); return(UnitParser.Default.TryParse <ReactivePowerUnit>(str, provider, out unit)); }