NSMassFormatter can't convert grams to lbs (NSUnitMass) or display value

前端 未结 2 1977
死守一世寂寞
死守一世寂寞 2021-01-26 04:57

I am trying to modernize my API calls and want to utilize NSMassFormatter. The problem is simple. I can\'t get any output (aiming for imperial locale). NSMeas

2条回答
  •  深忆病人
    2021-01-26 05:29

    The formatter does not perform any conversions. It merely formats a quantity. A gram is still a gram in any language.

    However, NSMassFormatter does provide a convenience method based on kilograms. So for example:

    let grams = 62453.0
    let measurement = Measurement(value: grams, unit: UnitMass.grams)
    let kilos = measurement.converted(to: .kilograms)
    let formatter = MassFormatter()
    formatter.isForPersonMassUse = true
    let s = formatter.string(fromKilograms: kilos.value)
    print(s) // "137.685 lb” on my machine
    

提交回复
热议问题