public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value) { var text = value as string; if (text != null) { PowerUnit result; if (PowerUnit.TryParse(text, out result)) { return(result); } var message = $"Could not convert the string '{text}' to an instance of PowerUnit)"; throw new NotSupportedException(message); } return(base.ConvertFrom(context, culture, value)); }
public string ToString(string format, IFormatProvider formatProvider, PowerUnit unit) { var quantity = unit.FromSiUnit(this.watts); return(string.Format("{0}{1}", quantity.ToString(format, formatProvider), unit.Symbol)); }
/// <summary> /// Initializes a new instance of <see cref="T:Gu.Units.Power"/>. /// </summary> /// <param name="value"></param> /// <param name="unit"><see cref="T:Gu.Units.Watts"/>.</param> public Power(double value, PowerUnit unit) { this.watts = unit.ToSiUnit(value); }
/// <summary> /// Creates a new instance of <see cref="T:Gu.Units.Power"/>. /// </summary> /// <param name="value"></param> /// <param name="unit"></param> public static Power From(double value, PowerUnit unit) { return(new Power(unit.ToSiUnit(value))); }
/// <summary> /// Returns a quantity indicating whether this instance is equal to a specified <see cref="Gu.Units.PowerUnit"/> object. /// </summary> /// <param name="other">An instance of <see cref="Gu.Units.PowerUnit"/> object to compare with this instance.</param> /// <returns> /// true if <paramref name="other"/> represents the same PowerUnit as this instance; otherwise, false. /// </returns> public bool Equals(PowerUnit other) { return(this.symbol == other.symbol); }
/// <summary> /// Creates an instance of <see cref="Gu.Units.PowerUnit"/> from its string representation /// </summary> /// <param name="text">The string representation of the <see cref="Gu.Units.PowerUnit"/></param> /// <param name="result">The parsed <see cref="PowerUnit"/></param> /// <returns>True if an instance of <see cref="PowerUnit"/> could be parsed from <paramref name="text"/></returns> public static bool TryParse(string text, out PowerUnit result) { return(UnitParser <PowerUnit> .TryParse(text, out result)); }