我正在制作一个单元转换器应用程序。在处理压力时,我发现基金会已经预定义了几个单位:
UnitPressure.newtonsPerMetersSquared
UnitPressure.bars
UnitPressure.poundsForcePerSquareInch但是没有标准的大气压。(请注意,酒吧与标准大气压并不相同,尽管它们是接近的)。
我在想如何处理这个案子?
发布于 2022-04-04 19:06:41
您可以非常容易地定义您自己的单位:
extension UnitPressure {
static let standardAtmospheres = UnitPressure(
symbol: "atm",
converter: UnitConverterLinear(coefficient: 101325)
)
}
Measurement(value: 1, unit: UnitPressure.bars)
.converted(to: .standardAtmospheres) // 0.9869232667160128 atm
Measurement(value: 1, unit: UnitPressure.standardAtmospheres)
.converted(to: .bars) // 1.01325 bar标准大气的符号是" atm“和WolframAlpha建议的1 atm= 101325 N/m^2,因此代码中使用了神奇的数字。
注意,N/m^2是UnitPressure的基本单位。
UnitPressure类将其基单元定义为newtonsPerMetersSquared,并提供以下单元,UnitConverterLinear转换器使用给定的系数初始化这些单元:.
https://stackoverflow.com/questions/71742152
复制相似问题