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 alargerand
smallerunit such as
ft, inor
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) orft
(UnitLength).Declaration
Swift
public let largeUnit: UnitType -
The smaller imperial unit. For example,
oz
(UnitMass) orin
(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) or1 ft = 12 in
(UnitLength).Declaration
Swift
public let smallToLargeConversion: Int -
Convert the input value to a
Measurementof the same unit type. This will always return the measurement converted to thebaseUnitand can be used to convert any value that is a Number orMeasurement. If this is a Number, then it is assumed that the unit for the number is thebaseUnit.Declaration
Swift
public func measurement(from value: Any) -> Measurement<UnitType>?Parameters
valueThe value to convert.
Return Value
A measurement in the appropriate unit or
nilif unable to convert. -
Convert the input US Customary unit values to a
Measurementof the same unit type. This will convert thelargeValueto aMeasurementwith a unit oflargeUnitand thesmallValueto aMeasurementwith a unit ofsmallUnit. The measurements will be summed and the total will be returned with a unit ofbaseUnit.Declaration
Swift
public func measurement(fromLargeValue largeValue: Double, smallValue: Double) -> Measurement<UnitType>Parameters
largeValueThe larger value to convert.
smallValueThe 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
valueThe value to convert.
Return Value
- largeValue: The larger component of the converted measurement.
- smallValue: The smaller component of the converted measurement.
View on GitHub
USCustomaryUnitConverter Structure Reference