public string ToString(PowerDensityUnit 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="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 PowerDensity 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 <PowerDensity, PowerDensityUnit>(str, provider, delegate(string value, string unit, IFormatProvider formatProvider2) { double parsedValue = double.Parse(value, formatProvider2); PowerDensityUnit parsedUnit = ParseUnit(unit, formatProvider2); return From(parsedValue, parsedUnit); }, (x, y) => FromWattsPerCubicMeter(x.WattsPerCubicMeter + y.WattsPerCubicMeter))); }
public string ToString(PowerDensityUnit 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> /// 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(PowerDensityUnit unit, [CanBeNull] string cultureName) { return(ToString(unit, cultureName, 2)); }
/// <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(PowerDensityUnit unit, [CanBeNull] IFormatProvider provider) { return(ToString(unit, provider, 2)); }
public static string GetAbbreviation(PowerDensityUnit unit, [CanBeNull] IFormatProvider provider) { provider = provider ?? UnitSystem.DefaultCulture; return(UnitSystem.GetCached(provider).GetDefaultAbbreviation(unit)); }
/// <summary> /// Dynamically convert from value and unit enum <see cref="PowerDensityUnit" /> to <see cref="PowerDensity" />. /// </summary> /// <param name="value">Value to convert from.</param> /// <param name="fromUnit">Unit to convert from.</param> /// <returns>PowerDensity unit value.</returns> public static PowerDensity?From(QuantityValue?value, PowerDensityUnit fromUnit) { return(value.HasValue ? new PowerDensity((double)value.Value, fromUnit) : default(PowerDensity?)); }
private double GetValueAs(PowerDensityUnit unit) { if (Unit == unit) { return(_value); } var baseUnitValue = GetValueInBaseUnit(); switch (unit) { case PowerDensityUnit.DecawattPerCubicFoot: return((baseUnitValue / 3.531466672148859e1) / 1e1d); case PowerDensityUnit.DecawattPerCubicInch: return((baseUnitValue / 6.102374409473228e4) / 1e1d); case PowerDensityUnit.DecawattPerCubicMeter: return((baseUnitValue) / 1e1d); case PowerDensityUnit.DecawattPerLiter: return((baseUnitValue / 1.0e3) / 1e1d); case PowerDensityUnit.DeciwattPerCubicFoot: return((baseUnitValue / 3.531466672148859e1) / 1e-1d); case PowerDensityUnit.DeciwattPerCubicInch: return((baseUnitValue / 6.102374409473228e4) / 1e-1d); case PowerDensityUnit.DeciwattPerCubicMeter: return((baseUnitValue) / 1e-1d); case PowerDensityUnit.DeciwattPerLiter: return((baseUnitValue / 1.0e3) / 1e-1d); case PowerDensityUnit.GigawattPerCubicFoot: return((baseUnitValue / 3.531466672148859e1) / 1e9d); case PowerDensityUnit.GigawattPerCubicInch: return((baseUnitValue / 6.102374409473228e4) / 1e9d); case PowerDensityUnit.GigawattPerCubicMeter: return((baseUnitValue) / 1e9d); case PowerDensityUnit.GigawattPerLiter: return((baseUnitValue / 1.0e3) / 1e9d); case PowerDensityUnit.KilowattPerCubicFoot: return((baseUnitValue / 3.531466672148859e1) / 1e3d); case PowerDensityUnit.KilowattPerCubicInch: return((baseUnitValue / 6.102374409473228e4) / 1e3d); case PowerDensityUnit.KilowattPerCubicMeter: return((baseUnitValue) / 1e3d); case PowerDensityUnit.KilowattPerLiter: return((baseUnitValue / 1.0e3) / 1e3d); case PowerDensityUnit.MegawattPerCubicFoot: return((baseUnitValue / 3.531466672148859e1) / 1e6d); case PowerDensityUnit.MegawattPerCubicInch: return((baseUnitValue / 6.102374409473228e4) / 1e6d); case PowerDensityUnit.MegawattPerCubicMeter: return((baseUnitValue) / 1e6d); case PowerDensityUnit.MegawattPerLiter: return((baseUnitValue / 1.0e3) / 1e6d); case PowerDensityUnit.MicrowattPerCubicFoot: return((baseUnitValue / 3.531466672148859e1) / 1e-6d); case PowerDensityUnit.MicrowattPerCubicInch: return((baseUnitValue / 6.102374409473228e4) / 1e-6d); case PowerDensityUnit.MicrowattPerCubicMeter: return((baseUnitValue) / 1e-6d); case PowerDensityUnit.MicrowattPerLiter: return((baseUnitValue / 1.0e3) / 1e-6d); case PowerDensityUnit.MilliwattPerCubicFoot: return((baseUnitValue / 3.531466672148859e1) / 1e-3d); case PowerDensityUnit.MilliwattPerCubicInch: return((baseUnitValue / 6.102374409473228e4) / 1e-3d); case PowerDensityUnit.MilliwattPerCubicMeter: return((baseUnitValue) / 1e-3d); case PowerDensityUnit.MilliwattPerLiter: return((baseUnitValue / 1.0e3) / 1e-3d); case PowerDensityUnit.NanowattPerCubicFoot: return((baseUnitValue / 3.531466672148859e1) / 1e-9d); case PowerDensityUnit.NanowattPerCubicInch: return((baseUnitValue / 6.102374409473228e4) / 1e-9d); case PowerDensityUnit.NanowattPerCubicMeter: return((baseUnitValue) / 1e-9d); case PowerDensityUnit.NanowattPerLiter: return((baseUnitValue / 1.0e3) / 1e-9d); case PowerDensityUnit.PicowattPerCubicFoot: return((baseUnitValue / 3.531466672148859e1) / 1e-12d); case PowerDensityUnit.PicowattPerCubicInch: return((baseUnitValue / 6.102374409473228e4) / 1e-12d); case PowerDensityUnit.PicowattPerCubicMeter: return((baseUnitValue) / 1e-12d); case PowerDensityUnit.PicowattPerLiter: return((baseUnitValue / 1.0e3) / 1e-12d); case PowerDensityUnit.TerawattPerCubicFoot: return((baseUnitValue / 3.531466672148859e1) / 1e12d); case PowerDensityUnit.TerawattPerCubicInch: return((baseUnitValue / 6.102374409473228e4) / 1e12d); case PowerDensityUnit.TerawattPerCubicMeter: return((baseUnitValue) / 1e12d); case PowerDensityUnit.TerawattPerLiter: return((baseUnitValue / 1.0e3) / 1e12d); case PowerDensityUnit.WattPerCubicFoot: return(baseUnitValue / 3.531466672148859e1); case PowerDensityUnit.WattPerCubicInch: return(baseUnitValue / 6.102374409473228e4); case PowerDensityUnit.WattPerCubicMeter: return(baseUnitValue); case PowerDensityUnit.WattPerLiter: return(baseUnitValue / 1.0e3); default: throw new NotImplementedException($"Can not convert {Unit} to {unit}."); } }
/// <summary> /// Converts this Duration to another Duration with the unit representation <paramref name="unit" />. /// </summary> /// <returns>A Duration with the specified unit.</returns> public PowerDensity ToUnit(PowerDensityUnit unit) { var convertedValue = GetValueAs(unit); return(new PowerDensity(convertedValue, unit)); }
/// <summary> /// Convert to the unit representation <paramref name="unit" />. /// </summary> /// <returns>Value converted to the specified unit.</returns> public double As(PowerDensityUnit unit) => GetValueAs(unit);
/// <summary> /// Dynamically convert from value and unit enum <see cref="PowerDensityUnit" /> to <see cref="PowerDensity" />. /// </summary> /// <param name="value">Value to convert from.</param> /// <param name="fromUnit">Unit to convert from.</param> /// <returns>PowerDensity unit value.</returns> public static PowerDensity From(double value, PowerDensityUnit fromUnit) { return(new PowerDensity(value, fromUnit)); }
/// <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 PowerDensity(double value, PowerDensityUnit unit) { _value = value; _unit = unit; }
public static void HasConversion(this PropertyBuilder <PowerDensity> propertyBuilder, PowerDensityUnit unit) => propertyBuilder.HasConversion(v => v.As(unit), v => new PowerDensity(v, unit));