/// <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 FuelEfficiency(decimal value, FuelEfficiencyUnit unit) { if (unit == FuelEfficiencyUnit.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="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 FuelEfficiency(double value, FuelEfficiencyUnit unit) { if (unit == FuelEfficiencyUnit.Undefined) { throw new ArgumentException("The quantity can not be created with an undefined unit.", nameof(unit)); } _value = Guard.EnsureValidNumber(value, nameof(value)); _unit = unit; }
// ReSharper restore VirtualMemberNeverOverriden.Global protected (double UnitsInBaseUnit, double Tolerence) GetConversionFactor(FuelEfficiencyUnit unit) { return(unit switch { FuelEfficiencyUnit.KilometerPerLiter => (KilometersPerLitersInOneLiterPer100Kilometers, KilometersPerLitersTolerance), FuelEfficiencyUnit.LiterPer100Kilometers => (LitersPer100KilometersInOneLiterPer100Kilometers, LitersPer100KilometersTolerance), FuelEfficiencyUnit.MilePerUkGallon => (MilesPerUkGallonInOneLiterPer100Kilometers, MilesPerUkGallonTolerance), FuelEfficiencyUnit.MilePerUsGallon => (MilesPerUsGallonInOneLiterPer100Kilometers, MilesPerUsGallonTolerance), _ => throw new NotSupportedException() });
/// <summary> /// Convert to the unit representation <paramref name="unit" />. /// </summary> /// <returns>Value converted to the specified unit.</returns> public double As(FuelEfficiencyUnit unit) { if (Unit == unit) { return(Convert.ToDouble(Value)); } var converted = AsBaseNumericType(unit); return(Convert.ToDouble(converted)); }
private double AsBaseNumericType(FuelEfficiencyUnit unit) { if (Unit == unit) { return(_value); } var baseUnitValue = AsBaseUnit(); switch (unit) { case FuelEfficiencyUnit.KilometerPerLiter: return(100 / baseUnitValue); case FuelEfficiencyUnit.LiterPer100Kilometers: return(baseUnitValue); case FuelEfficiencyUnit.MilePerUkGallon: return((100 * 4.54609188) / (1.609344 * baseUnitValue)); case FuelEfficiencyUnit.MilePerUsGallon: return((100 * 3.785411784) / (1.609344 * baseUnitValue)); default: throw new NotImplementedException($"Can not convert {Unit} to {unit}."); } }
/// <summary> /// Converts this FuelEfficiency to another FuelEfficiency with the unit representation <paramref name="unit" />. /// </summary> /// <returns>A FuelEfficiency with the specified unit.</returns> public FuelEfficiency ToUnit(FuelEfficiencyUnit unit) { var convertedValue = AsBaseNumericType(unit); return(new FuelEfficiency(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 FuelEfficiencyUnit unit) { IFormatProvider provider = GetFormatProviderFromCultureName(cultureName); return(UnitParser.Default.TryParse <FuelEfficiencyUnit>(str, provider, out unit)); }
public static bool TryParseUnit(string str, out FuelEfficiencyUnit unit) { return(TryParseUnit(str, null, out unit)); }
public static FuelEfficiency From(double value, FuelEfficiencyUnit fromUnit) { return(new FuelEfficiency((double)value, fromUnit)); }
/// <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(FuelEfficiencyUnit unit, [CanBeNull] string cultureName) { IFormatProvider provider = GetFormatProviderFromCultureName(cultureName); return(UnitAbbreviationsCache.Default.GetDefaultAbbreviation(unit, provider)); }
/// <summary> /// Get unit abbreviation string. /// </summary> /// <param name="unit">Unit to get abbreviation for.</param> /// <returns>Unit abbreviation string.</returns> public static string GetAbbreviation(FuelEfficiencyUnit unit) { return(GetAbbreviation(unit, null)); }
/// <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 FuelEfficiency(double value, FuelEfficiencyUnit unit) { _value = value; _unit = 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 FuelEfficiency ToUnit(FuelEfficiencyUnit unit) { var convertedValue = GetValueAs(unit); return(new FuelEfficiency(convertedValue, unit)); }
/// <summary> /// Convert to the unit representation <paramref name="unit" />. /// </summary> /// <returns>Value converted to the specified unit.</returns> public double As(FuelEfficiencyUnit unit) => GetValueAs(unit);
public static FuelEfficiency From(decimal value, FuelEfficiencyUnit fromUnit) { return(new FuelEfficiency((decimal)value, fromUnit)); }