我有一个Measurement
属性:
let measurement = Measurement(value: 10000.0, unit: UnitMass.grams)
我想打印成字符串。我使用以下
MeasurementFormatter
:let measurementFormatter = MeasurementFormatter()
measurementFormatter.unitStyle = .short
measurementFormatter.numberFormatter.numberStyle = .decimal
print(measurementFormatter.string(from: measurement))
打印
10,000g
。如何让它打印
10,000 g
,即值和单位之间有空格? 最佳答案
问题是您正在使用样式.short
。只要注释掉这一行,它应该在它们之间显示一个空格:
let measurement = Measurement(value: 10000, unit: UnitMass.grams)
let measurementFormatter = MeasurementFormatter()
// measurementFormatter.unitStyle = .short
measurementFormatter.unitOptions = .providedUnit
measurementFormatter.numberFormatter.numberStyle = .decimal
print(measurementFormatter.string(from: measurement)) // "10,000 g\n"
关于swift - 度量格式器:值和单位之间的空格,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/43058277/