gpt4 book ai didi

swift - 测量格式化程序 : space between value and unit

转载 作者:行者123 更新时间:2023-11-28 15:45:56 25 4
gpt4 key购买 nike

我有一个 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 - 测量格式化程序 : space between value and unit,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43058277/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com