在iOS 10中使用适当大小的本地化单位
我正在使用iOS 10中的新版本获取用于长度测量的本地化字符串:在iOS 10中使用适当大小的本地化单位,ios,swift,units-of-measurement,Ios,Swift,Units Of Measurement,我正在使用iOS 10中的新版本获取用于长度测量的本地化字符串: let length = Measurement(value: 200, unit: UnitLength.centimeters) let str = MeasurementFormatter().string(from: length) 但是,对于我的(英国)语言环境,我会返回“0.001 mi”,这不是一个非常有用的字符串来显示。相反,我更希望得到一个使用适当单位(例如,“6.56英尺”或“2.19码”)测量值的字符串。A
let length = Measurement(value: 200, unit: UnitLength.centimeters)
let str = MeasurementFormatter().string(from: length)
但是,对于我的(英国)语言环境,我会返回
“0.001 mi”
,这不是一个非常有用的字符串来显示。相反,我更希望得到一个使用适当单位(例如,“6.56英尺”
或“2.19码”
)测量值的字符串。API是否可以实现这一点,或者我最好使用使用SI前缀的自定义解决方案?解决方案很简单:
let length = Measurement(value: 200, unit: UnitLength.centimeters)
let f = MeasurementFormatter()
f.unitOptions = MeasurementFormatter.UnitOptions.naturalScale
f.numberFormatter.maximumFractionDigits = 2
let str = MeasurementFormatter().string(from: length)
您需要将格式化程序的unitOptiojs属性转换为.naturalScale