public QuantityWithUnitGuiEnvironment(QuantityWithUnitGuiEnvironment from, IEnumerable <IUnit> additionalUnits) { _fixedUnits = from._fixedUnits; _defaultUnit = from._defaultUnit; _additionalUnits = new ObservableCollection <IUnit>(additionalUnits); CreateUnitListSortedByShortcutLengthDescending(); _additionalUnits.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(EhAdditionalUnits_CollectionChanged); }
/// <summary> /// Tries to get a prefixed unit from a shortcut, considering all units in this environment. /// </summary> /// <param name="shortCut">The shortcut. Can be a compound of prefix and unit, e.g. 'mA'. An empty string is converted to <see cref="Altaxo.Units.Dimensionless.Unity.Instance"/></param> /// <param name="result">If successfully, the resulting prefixed unit.</param> /// <returns>True if the conversion was successful; false otherwise.</returns> /// <exception cref="ArgumentNullException">s</exception> public bool TryGetPrefixedUnitFromShortcut(string shortCut, out IPrefixedUnit result) { if (null == shortCut) { throw new ArgumentNullException(nameof(shortCut)); } shortCut = shortCut.Trim(); if ("" == shortCut) // If string is empty, we consider this as a dimensionless unit "Unity" { result = new PrefixedUnit(SIPrefix.None, Altaxo.Units.Dimensionless.Unity.Instance); return(true); } SIPrefix prefix = null; foreach (IUnit u in UnitsSortedByShortcutLengthDescending) // for each unit { if (string.IsNullOrEmpty(u.ShortCut) || (!shortCut.EndsWith(u.ShortCut))) { continue; } var prefixString = shortCut.Substring(0, shortCut.Length - u.ShortCut.Length); if (prefixString.Length == 0) // if prefixString is empty, then it is the unit without prefix { result = new PrefixedUnit(SIPrefix.None, u); return(true); } prefix = SIPrefix.TryGetPrefixFromShortcut(prefixString); if (null != prefix) // we found a prefix, thus we can return prefix + unit { result = new PrefixedUnit(prefix, u); return(true); } } result = null; return(false); }
/// <summary>Converts this quantity to another quantity in the provided prefixed unit.</summary> /// <param name="prefixedUnit">The prefixed unit to convert the quantity to.</param> /// <returns>New instance of a quantity in the provided prefixed unit.</returns> public DimensionfulQuantity AsQuantityIn(IPrefixedUnit prefixedUnit) { return(AsQuantityIn(prefixedUnit.Prefix, prefixedUnit.Unit)); }
/// <summary>Creates a quantity with the provided value in the given prefixed unit.</summary> /// <param name="value">The value of the created quantity.</param> /// <param name="prefixedUnit">The prefixed unit of the created quanity.</param> public DimensionfulQuantity(double value, IPrefixedUnit prefixedUnit) { _value = value; _prefix = prefixedUnit.Prefix; _unit = prefixedUnit.Unit; }
/// <summary>Converts this quantity to its numerical value in the given unit, with the given prefix.</summary> /// <param name="prefixedUnit">The prefixed unit in which to get the numerical value of this quantity.</param> /// <returns>Numerical value of this quantity in the provided unit with the provided prefix.</returns> public double AsValueIn(IPrefixedUnit prefixedUnit) { return(AsValueIn(prefixedUnit.Prefix, prefixedUnit.Unit)); }