/// <summary> /// Converts the given amount to the given unit. /// </summary> public static Amount ConvertTo(Amount amount, Unit toUnit) { try { // Performance optimalization: if (Object.ReferenceEquals(amount.Unit, toUnit)) { return(amount); } // Perform conversion: if (amount.Unit.IsCompatibleTo(toUnit)) { return(new Amount(amount.Value * amount.Unit.Factor / toUnit.Factor, toUnit)); } else { UnitConversionKeySlot expectedSlot = new UnitConversionKeySlot(amount.Unit, toUnit); return(Instance._conversions[expectedSlot].Convert(amount).ConvertedTo(toUnit)); } } catch (KeyNotFoundException) { throw new UnitConversionException(amount.Unit, toUnit); } }
/// <summary> /// Converts the given amount to the given unit. /// </summary> public static Amount ConvertTo(Amount amount, Unit toUnit) { try { if (amount.Unit.CompatibleTo(toUnit)) { return(new Amount(amount.Value * amount.Unit.Factor / toUnit.Factor, toUnit)); } else { UnitConversionKeySlot expectedSlot = new UnitConversionKeySlot(amount.Unit, toUnit); return(Instance.conversions[expectedSlot].Convert(amount).ConvertedTo(toUnit)); } } catch (KeyNotFoundException) { throw new UnitConversionException(amount.Unit, toUnit); } }
public override bool Equals(object obj) { UnitConversionKeySlot other = obj as UnitConversionKeySlot; return((this.fromType == other.fromType) && (this.toType == other.toType)); }
/// <summary> /// Converts the given amount to the given unit. /// </summary> public static Amount ConvertTo(Amount amount, Unit toUnit) { try { if (amount.Unit.CompatibleTo(toUnit)) { return new Amount(amount.Value * amount.Unit.Factor / toUnit.Factor, toUnit); } else { UnitConversionKeySlot expectedSlot = new UnitConversionKeySlot(amount.Unit, toUnit); return Instance.conversions[expectedSlot].Convert(amount).ConvertedTo(toUnit); } } catch (KeyNotFoundException) { throw new UnitConversionException(amount.Unit, toUnit); } }