USCustomaryUnitConverter

public struct USCustomaryUnitConverter<UnitType> where UnitType : Dimension

USCustomaryUnitConverter is generic struct for converting measurements from various units.

Note

US Customary and Imperial units measurements are typically shown using a larger and smaller unit such as ft, in or lb, oz. This converter will convert from a base unit (default will be metric) to two values, a larger and smaller unit, that are added together to represent a measurement. For example, 5 ft, 6 in.
  • The larger imperial unit. For example, lb (UnitMass) or ft (UnitLength).

    Declaration

    Swift

    public let largeUnit: UnitType
  • The smaller imperial unit. For example, oz (UnitMass) or in (UnitLength).

    Declaration

    Swift

    public let smallUnit: UnitType
  • The base unit. Default will be the metric value typically used for person measurements such as height or weight.

    Declaration

    Swift

    public var baseUnit: UnitType
  • The number of smaller units in the larger unit. For example, 1 lb = 12 oz (UnitMass) or 1 ft = 12 in (UnitLength).

    Declaration

    Swift

    public let smallToLargeConversion: Int
  • Convert the input value to a Measurement of the same unit type. This will always return the measurement converted to the baseUnit and can be used to convert any value that is a Number or Measurement. If this is a Number, then it is assumed that the unit for the number is the baseUnit.

    Declaration

    Swift

    public func measurement(from value: Any) -> Measurement<UnitType>?

    Parameters

    value

    The value to convert.

    Return Value

    A measurement in the appropriate unit or nil if unable to convert.

  • Convert the input US Customary unit values to a Measurement of the same unit type. This will convert the largeValue to a Measurement with a unit of largeUnit and the smallValue to a Measurement with a unit of smallUnit. The measurements will be summed and the total will be returned with a unit of baseUnit.

    Declaration

    Swift

    public func measurement(fromLargeValue largeValue: Double, smallValue: Double) -> Measurement<UnitType>

    Parameters

    largeValue

    The larger value to convert.

    smallValue

    The smaller value to convert.

    Return Value

    A measurement in the appropriate unit.

  • Convert the input value to a tuple with the large and small measurement components used to represent the measurement using US Customary or Imperial units where the units are represented with a larger and smaller unit.

    • example:

      // returns 1 foot, 8 inches
      let feet_1_AndInches_8 = lengthConverter.toTupleValue(from: 20 * 2.54)
      

    Declaration

    Swift

    public func toTupleValue(from value: Any) -> (largeValue: Double, smallValue: Double)?

    Parameters

    value

    The value to convert.

    Return Value

    • largeValue: The larger component of the converted measurement.
    • smallValue: The smaller component of the converted measurement.