public string ToString(ILengthUnit newUnits) { if (newUnits is null) { throw new ArgumentNullException(nameof(newUnits)); } if ((minSize <= 0.0 && maxSize <= 0.0) || newUnits is null) { return(UnknownSize); } if (minSize != maxSize) { // Represent the size as a range. LengthMeasurement minLength = new LengthMeasurement(minSize, this.units).To(newUnits); LengthMeasurement maxLength = new LengthMeasurement(maxSize, this.units).To(newUnits); return(string.Format("**{0}-{1} {2}**", minLength.ToString(LengthMeasurementFormat.Value), maxLength.ToString(LengthMeasurementFormat.Value), minLength.Units.ToString())); } else { // Represent the size as a single value. LengthMeasurement size = new LengthMeasurement(minSize <= 0.0 ? maxSize : minSize, this.units).To(newUnits); return(string.Format("**{0}**", size.ToString())); } }
public LengthMeasurement To(ILengthUnit units) { if (units.Name.Equals(Units.Name, StringComparison.OrdinalIgnoreCase)) { return(new LengthMeasurement(Value, Units)); } return(new LengthMeasurement(ToMeters() / units.MeterConversionFactor, units)); }
private static ILengthUnit GetKnownUnitOrThrow(string name) { ILengthUnit knownUnit = GetKnownUnit(name); if (knownUnit is null) { throw new ArgumentException("Unrecognized units."); } return(knownUnit); }
private static void RegisterUnit(IDictionary <string, ILengthUnit> dictionary, ILengthUnit unit) { if (!string.IsNullOrEmpty(unit.Name)) { string pluralName = ToPlural(unit.Name).Replace("-", " "); dictionary[unit.Name.ToLowerInvariant().Replace("-", " ")] = unit; dictionary[pluralName] = unit; } dictionary[unit.Abbreviation] = unit; }
public LengthMeasurement ToNearestUnits(LengthUnitType type, params string[] allowedUnits) { if (Units.Type == type) { return(new LengthMeasurement(Value, Units)); } // Find the unit with the closest conversion factor to ours. ILengthUnit unit = Units.ToNearestUnits(type, allowedUnits); return(To(unit)); }
public ILengthUnit ToNearestUnits(LengthUnitType type, params string[] allowedUnits) { if (Type == type) { return(new LengthUnit(this)); } // Find the unit with the closest conversion factor to ours. ILengthUnit result = knownUnits.Value.Values .Where(unit => unit.Type == type) .Where(unit => allowedUnits.Count() <= 0 || allowedUnits.Any(allowedUnit => unit.Equals(allowedUnit))) .OrderBy(unitu => Math.Abs(MeterConversionFactor - unitu.MeterConversionFactor)) .FirstOrDefault(); return(result); }
public LengthMeasurement(double value, ILengthUnit units) { this.Value = value; this.Units = units; }
/// <summary> /// Unit Strings, such as length, volume, ... /// </summary> /// <param name="prop"></param> /// <returns></returns> private static string GetUnitString(IProperty prop) { IUnitBase unitBase = prop.Unit; ITimeUnit timeUnit = unitBase as ITimeUnit; if (timeUnit != null) { return(timeUnit.Time.ToString()); } ITravelRateUnit travalrateunit = unitBase as ITravelRateUnit; if (travalrateunit != null) { return(travalrateunit.TravelRate.ToString()); } ILengthUnit lengthunit = unitBase as ILengthUnit; if (lengthunit != null) { return(lengthunit.Length.ToString()); } ICurrencyUnit currencyunit = unitBase as ICurrencyUnit; if (currencyunit != null) { return(currencyunit.Currency.ToString()); } IVolumeUnit volumeunit = unitBase as IVolumeUnit; if (volumeunit != null) { return(volumeunit.Volume.ToString()); } IMassUnit massunit = unitBase as IMassUnit; if (massunit != null) { return(massunit.Mass.ToString()); } IVolumeFlowRateUnit volumeflowrateunit = unitBase as IVolumeFlowRateUnit; if (volumeflowrateunit != null) { return(volumeflowrateunit.Volume.ToString() + "/" + volumeflowrateunit.Time.ToString()); } IMassFlowRateUnit massflowrateunit = unitBase as IMassFlowRateUnit; if (massflowrateunit != null) { return(massflowrateunit.Mass.ToString() + "/" + massflowrateunit.Time.ToString()); } ITravelAccelerationUnit timeaccelerationunit = unitBase as ITravelAccelerationUnit; if (timeaccelerationunit != null) { return(timeaccelerationunit.Length.ToString() + "/" + timeaccelerationunit.Time.ToString()); } ICurrencyPerTimeUnit currencepertimeunit = unitBase as ICurrencyPerTimeUnit; if (currencepertimeunit != null) { return(currencepertimeunit.CurrencyPerTimeUnit.ToString()); } return("none"); }
public static bool TryParse(string input, out ILengthUnit result) { result = GetKnownUnit(input); return(result != null); }
public LengthUnit(ILengthUnit other) : this(other.Name, other.Abbreviation, other.MeterConversionFactor, other.Type) { }