public void EnergyConversions(double value1, EnergyUnit units1, double value2, EnergyUnit units2) { new Energy(value1, units1) { Units = units2 }.Value.ShouldBeWithinEpsilonOf(value2); new Energy(value2, units2) { Units = units1 }.Value.ShouldBeWithinEpsilonOf(value1); }
/// <summary> /// Dynamically convert from value and unit enum <see cref="EnergyUnit" /> to <see cref="Energy" />. /// </summary> /// <param name="value">Value to convert from.</param> /// <param name="fromUnit">Unit to convert from.</param> /// <returns>Energy unit value.</returns> public static Energy?From(QuantityValue?value, EnergyUnit fromUnit) { return(value.HasValue ? new Energy((double)value.Value, fromUnit) : default(Energy?)); }
public static string GetAbbreviation(EnergyUnit unit) { return(GetAbbreviation(unit, null)); }
/// <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(EnergyUnit unit) { return(ToString(unit, null, 2)); }
private double GetValueAs(EnergyUnit unit) { if (Unit == unit) { return(_value); } var baseUnitValue = GetValueInBaseUnit(); switch (unit) { case EnergyUnit.BritishThermalUnit: return(baseUnitValue / 1055.05585262); case EnergyUnit.Calorie: return(baseUnitValue / 4.184); case EnergyUnit.DecathermEc: return((baseUnitValue / 1.05505585262e8) / 1e1d); case EnergyUnit.DecathermImperial: return((baseUnitValue / 1.05505585257348e8) / 1e1d); case EnergyUnit.DecathermUs: return((baseUnitValue / 1.054804e8) / 1e1d); case EnergyUnit.ElectronVolt: return(baseUnitValue / 1.602176565e-19); case EnergyUnit.Erg: return(baseUnitValue / 1e-7); case EnergyUnit.FootPound: return(baseUnitValue / 1.355817948); case EnergyUnit.GigabritishThermalUnit: return((baseUnitValue / 1055.05585262) / 1e9d); case EnergyUnit.GigaelectronVolt: return((baseUnitValue / 1.602176565e-19) / 1e9d); case EnergyUnit.Gigajoule: return((baseUnitValue) / 1e9d); case EnergyUnit.GigawattDay: return((baseUnitValue / (24 * 3600d)) / 1e9d); case EnergyUnit.GigawattHour: return((baseUnitValue / 3600d) / 1e9d); case EnergyUnit.HorsepowerHour: return(baseUnitValue / 2.6845195377e6); case EnergyUnit.Joule: return(baseUnitValue); case EnergyUnit.KilobritishThermalUnit: return((baseUnitValue / 1055.05585262) / 1e3d); case EnergyUnit.Kilocalorie: return((baseUnitValue / 4.184) / 1e3d); case EnergyUnit.KiloelectronVolt: return((baseUnitValue / 1.602176565e-19) / 1e3d); case EnergyUnit.Kilojoule: return((baseUnitValue) / 1e3d); case EnergyUnit.KilowattDay: return((baseUnitValue / (24 * 3600d)) / 1e3d); case EnergyUnit.KilowattHour: return((baseUnitValue / 3600d) / 1e3d); case EnergyUnit.MegabritishThermalUnit: return((baseUnitValue / 1055.05585262) / 1e6d); case EnergyUnit.Megacalorie: return((baseUnitValue / 4.184) / 1e6d); case EnergyUnit.MegaelectronVolt: return((baseUnitValue / 1.602176565e-19) / 1e6d); case EnergyUnit.Megajoule: return((baseUnitValue) / 1e6d); case EnergyUnit.MegawattDay: return((baseUnitValue / (24 * 3600d)) / 1e6d); case EnergyUnit.MegawattHour: return((baseUnitValue / 3600d) / 1e6d); case EnergyUnit.Millijoule: return((baseUnitValue) / 1e-3d); case EnergyUnit.TeraelectronVolt: return((baseUnitValue / 1.602176565e-19) / 1e12d); case EnergyUnit.TerawattDay: return((baseUnitValue / (24 * 3600d)) / 1e12d); case EnergyUnit.TerawattHour: return((baseUnitValue / 3600d) / 1e12d); case EnergyUnit.ThermEc: return(baseUnitValue / 1.05505585262e8); case EnergyUnit.ThermImperial: return(baseUnitValue / 1.05505585257348e8); case EnergyUnit.ThermUs: return(baseUnitValue / 1.054804e8); case EnergyUnit.WattDay: return(baseUnitValue / (24 * 3600d)); case EnergyUnit.WattHour: return(baseUnitValue / 3600d); 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 Energy ToUnit(EnergyUnit unit) { var convertedValue = GetValueAs(unit); return(new Energy(convertedValue, unit)); }
/// <summary> /// Dynamically convert from value and unit enum <see cref="EnergyUnit" /> to <see cref="Energy" />. /// </summary> /// <param name="value">Value to convert from.</param> /// <param name="fromUnit">Unit to convert from.</param> /// <returns>Energy unit value.</returns> public static Energy From(double value, EnergyUnit fromUnit) { return(new Energy(value, fromUnit)); }
public string ToString(EnergyUnit unit, CultureInfo culture = null, int significantDigitsAfterRadix = 2) { return(ToString(unit, culture, UnitFormatter.GetFormat(As(unit), significantDigitsAfterRadix))); }
public static bool TryParseUnit(string str, out EnergyUnit unit) { return(TryParseUnit(str, null, out unit)); }
/// <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(EnergyUnit unit, [CanBeNull] string cultureName) { IFormatProvider provider = GetFormatProviderFromCultureName(cultureName); return(UnitAbbreviationsCache.Default.GetDefaultAbbreviation(unit, provider)); }
public static string AverageConsumed(EnergyUnit unit) => $"Avg {EnergyConsumed(unit)}";
public static string EnergyConsumed(EnergyUnit unit) => $"Consumed ({unit})";
public static string AverageActiveEnergy(EnergyUnit unit) => $"Average {ActiveEnergy(unit)}";
public static string AverageBasalEnergy(EnergyUnit unit) => $"Average {BasalEnergy(unit)}";
public static string ActiveEnergy(EnergyUnit unit) => $"Active Energy ({unit})";
public static string BasalEnergy(EnergyUnit unit) => $"Basal Energy ({unit})";
public static string GetAbbreviation(EnergyUnit unit, CultureInfo culture = null) { return(UnitSystem.GetCached(culture).GetDefaultAbbreviation(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 EnergyUnit unit) { IFormatProvider provider = GetFormatProviderFromCultureName(cultureName); return(UnitParser.Default.TryParse <EnergyUnit>(str, provider, out unit)); }
public string ToString(EnergyUnit unit, CultureInfo culture, string format, params object[] args) { return(string.Format(culture, format, UnitFormatter.GetFormatArgs(unit, As(unit), culture, args))); }
/// <summary> /// Converts this Energy to another Energy with the unit representation <paramref name="unit" />. /// </summary> /// <returns>A Energy with the specified unit.</returns> public Energy ToUnit(EnergyUnit unit) { var convertedValue = AsBaseNumericType(unit); return(new Energy(convertedValue, unit)); }
/// <summary> /// Convert to the unit representation <paramref name="unit" />. /// </summary> /// <returns>Value converted to the specified unit.</returns> public double As(EnergyUnit unit) => GetValueAs(unit);
private void CustomizeFixture(Fixture fixture) { // Customize common fixture setup here fixture.Customize <number>(e => e.FromFactory(() => fixture.CreateInRange(-100, 100))); fixture.Customize <TimeSpan>(e => e.FromFactory(() => TimeSpan.FromSeconds(fixture.Create <double>()))); fixture.Customize <Time>(e => e.FromFactory(() => new Time(fixture.Create <number>()))); fixture.Customize <WeightUnit>(e => e.FromFactory(() => fixture.CreateFromSet(WeightUnit.GetPredefinedUnits()))); fixture.Customize <Weight>(e => e.FromFactory(() => new Weight(fixture.Create <number>()).Convert(fixture.Create <WeightUnit>()))); fixture.Customize <LengthUnit>(e => e.FromFactory(() => fixture.CreateFromSet(LengthUnit.GetPredefinedUnits()))); fixture.Customize <Length>(e => e.FromFactory(() => new Length(fixture.Create <number>()).Convert(fixture.Create <LengthUnit>()))); fixture.Customize <SpeedUnit>(e => e.FromFactory(() => fixture.CreateFromSet(SpeedUnit.GetPredefinedUnits()))); fixture.Customize <Speed>(e => e.FromFactory(() => new Speed(fixture.Create <number>()).Convert(fixture.Create <SpeedUnit>()))); fixture.Customize <AreaUnit>(e => e.FromFactory(() => fixture.CreateFromSet(AreaUnit.GetPredefinedUnits()))); fixture.Customize <Area>(e => e.FromFactory(() => new Area(fixture.Create <number>()).Convert(fixture.Create <AreaUnit>()))); fixture.Customize <VolumeUnit>(e => e.FromFactory(() => fixture.CreateFromSet(VolumeUnit.GetPredefinedUnits()))); fixture.Customize <Volume>(e => e.FromFactory(() => new Volume(fixture.Create <number>()).Convert(fixture.Create <VolumeUnit>()))); fixture.Customize <PowerUnit>(e => e.FromFactory(() => fixture.CreateFromSet(PowerUnit.GetPredefinedUnits()))); fixture.Customize <Power>(e => e.FromFactory(() => new Power(fixture.Create <number>()).Convert(fixture.Create <PowerUnit>()))); fixture.Customize <EnergyUnit>(e => e.FromFactory(() => fixture.CreateFromSet(EnergyUnit.GetPredefinedUnits()))); fixture.Customize <Energy>(e => e.FromFactory(() => new Energy(fixture.Create <number>()).Convert(fixture.Create <EnergyUnit>()))); fixture.Customize <AngleUnit>(e => e.FromFactory(() => fixture.CreateFromSet(AngleUnit.GetPredefinedUnits()))); fixture.Customize <Angle>(e => e.FromFactory(() => new Angle(fixture.Create <number>()).Convert(fixture.Create <AngleUnit>()))); fixture.Customize <DegreeAngle>(e => e.FromFactory(() => new DegreeAngle(fixture.Create <number>()))); fixture.Customize <RadianAngle>(e => e.FromFactory(() => new RadianAngle(fixture.Create <number>()))); fixture.Customize <GeoCoordinate>(e => e.FromFactory(() => new GeoCoordinate( latitude: fixture.CreateInRange(GeoCoordinate.MinLatitude.TotalDegrees, GeoCoordinate.MaxLatitude.TotalDegrees), longitude: fixture.CreateInRange(GeoCoordinate.MinLongitude.TotalDegrees, GeoCoordinate.MaxLongitude.TotalDegrees)))); }
/// <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 Energy(double value, EnergyUnit unit) { _value = value; _unit = unit; }
public static void HasConversion(this PropertyBuilder <Energy> propertyBuilder, EnergyUnit unit) => propertyBuilder.HasConversion(v => v.As(unit), v => new Energy(v, unit));
public void SetEnergyUnit(EnergyUnit unit) => Preferences.Set(PreferenceKeys.EnergyUnit, unit.ToString());
public static string GetAbbreviation(EnergyUnit unit, [CanBeNull] IFormatProvider provider) { provider = provider ?? UnitSystem.DefaultCulture; return(UnitSystem.GetCached(provider).GetDefaultAbbreviation(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="culture">Culture to use for localization and number formatting.</param> /// <returns>String representation.</returns> public string ToString(EnergyUnit unit, [CanBeNull] Culture culture) { return(ToString(unit, culture, 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(EnergyUnit unit, [CanBeNull] IFormatProvider provider) { return(ToString(unit, provider, 2)); }
public static string GetAbbreviation(EnergyUnit unit, [CanBeNull] Culture culture) { return(UnitSystem.GetCached(culture).GetDefaultAbbreviation(unit)); }
/// <summary> /// Initializes a new instance of <see cref="Gu.Units.Wpf.EnergyConverter"/>. /// </summary> /// <param name="unit"><see cref="Gu.Units.EnergyUnit"/>.</param> public EnergyConverter(EnergyUnit unit) { Unit = 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(EnergyUnit unit, [CanBeNull] string cultureName) { return(ToString(unit, cultureName, 2)); }
protected static string CreateSuffix(SymbolFormat format, EnergyUnit unit) { return default(Energy).ToString(unit, format).Trim('0'); }