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())); } }
private Tuple <LengthMeasurement, LengthMeasurement> GetMetricAndImperialMeasurements(double sizeValue) { LengthMeasurement metricLength; LengthMeasurement imperialLength; // If the given length is >= 1 meter, show the length as meters. // If the given length >= 1 centimeter, show the length as centimeters. // If the unit is already metric, show the length as the given unit. // Otherwise, default to centimeters. if (sizeValue >= new LengthMeasurement(1, "m").To(units).Value) { metricLength = new LengthMeasurement(sizeValue, units).To("m"); } else if (sizeValue >= new LengthMeasurement(1, "cm").To(units).Value) { metricLength = new LengthMeasurement(sizeValue, units).To("cm"); } else if (units.Type == LengthUnitType.Metric) { metricLength = new LengthMeasurement(sizeValue, units); } else { metricLength = new LengthMeasurement(sizeValue, units).To("cm"); } // If the given length is >= 1 foot, show the length as feet. // If the given length >= 1 inch, show the length as inches. // If the unit is already imperial, show the length as the given unit. // Otherwise, default to inches. if (sizeValue >= new LengthMeasurement(1, "ft").To(units).Value) { imperialLength = new LengthMeasurement(sizeValue, units).To("ft"); } else if (sizeValue >= new LengthMeasurement(1, "in").To(units).Value) { imperialLength = new LengthMeasurement(sizeValue, units).To("in"); } else if (units.Type == LengthUnitType.Imperial) { imperialLength = new LengthMeasurement(sizeValue, units); } else { imperialLength = new LengthMeasurement(sizeValue, units).To("in"); } return(new Tuple <LengthMeasurement, LengthMeasurement>(metricLength, imperialLength)); }