我正在制作一个单位转换器应用程序.在处理压力时,我发现在Foundation中已经预定义了几个单元:
UnitPressure.newtonsPerMetersSquared
UnitPressure.bars
UnitPressure.poundsForcePerSquareInch
但是没有标准的大气压.(请注意,bar与标准大气压力不同,尽管它们很接近).
我想知道我该怎么处理这个案子?
我正在制作一个单位转换器应用程序.在处理压力时,我发现在Foundation中已经预定义了几个单元:
UnitPressure.newtonsPerMetersSquared
UnitPressure.bars
UnitPressure.poundsForcePerSquareInch
但是没有标准的大气压.(请注意,bar与标准大气压力不同,尽管它们很接近).
我想知道我该怎么处理这个案子?
你可以很容易地定义自己的单位:
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",1 atm=101325 N/m^2,如WolframAlpha所示,因此代码中使用了幻数.
请注意,N/m^2是UnitPressure
的基本单位.
UnitPressure
类将其基本单位定义为newtonsPerMetersSquared
,并提供以下单位,UnitConverterLinear
个转换器使用给定的系数进行初始化:[……]