一、单位转换
let heightFeet =Measurement(value:6,unit:UnitLength.feet)
let heightInches = heightFeet.converted(to:UnitLength.inches) //英寸 1英尺等于12英寸
let heightSensible = heightFeet.converted(to:UnitLength.meters) //米 1英尺等于0.3048米
print("------")
print(heightFeet.description)
//值
print(heightFeet.value)
//符号
print(heightFeet.unit.symbol)
print(heightInches)
//值
print(heightInches.value)
//符号
print(heightInches.unit.symbol)
print(heightSensible)
print("------")
二、格式化local
let length =Measurement(value:6,unit:UnitLength.meters)
let formatter1 =MeasurementFormatter()
formatter1.locale= NSLocale.system
print(formatter1.string(from: length))
三、格式化
letformatter =MeasurementFormatter()
formatter.unitOptions= .providedUnit
letnumberFormatter =NumberFormatter()
numberFormatter.minimumIntegerDigits=1//小数前至少一位数字
formatter.numberFormatter.minimumFractionDigits = 1 //小数点后保留一位
formatter.numberFormatter.maximumFractionDigits = 2 //小数点后最多保留一位
formatter.unitStyle= .long
print(formatter.string(from: heightInches))
//72.0 in 原值:72.00000000000001 in
print(formatter.string(from: heightInches + heightFeet))
//3.66 m 原值: 3.6576000000000004 m
四、所有打印
------
6.0 ft
6.0
ft
72.00000000000001 in
72.00000000000001
in
1.8288000000000002 m
------
0.004 mi
72.0 inches
3.66 meters